How to add checkboxes to JTABLE swing
109,195
1) JTable knows JCheckbox with built-in Boolean TableCellRenderers and TableCellEditor by default, then there is contraproductive declare something about that,
2) AbstractTableModel should be useful, where is in the JTable
required to reduce/restrict/change nested and inherits methods by default implemented in the DefaultTableModel
,
3) consider using DefaultTableModel
, (if you are not sure about how to works) instead of AbstractTableModel
,
could be generated from simple code:
import javax.swing.*;
import javax.swing.table.*;
public class TableCheckBox extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public TableCheckBox() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
/*@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}*/
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Integer.class;
case 3:
return Double.class;
default:
return Boolean.class;
}
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableCheckBox frame = new TableCheckBox();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on October 17, 2020Comments
-
Admin over 3 years
Does anyone know how to put a
JCheckBox
in aJTable
column? Something like this:I took this from How To use Tables
Thanks in advance.
-
kleopatra almost 13 years-1 for subclassing the table for data-related reasons: it's clearly the responsibility of the model to return the correct classes, the view must not second guess that
-
mKorbel almost 13 years1) I never heard about subClassing issues, seriously do not understand, it might be appropriate to clarify the impetus with posting here a bright example, or link to with detailed subscription, 2) maybe there deepest reasin for why I block non-subclassing code just and only about swift level for OP , 3) imaging that, :-)
-
Dennis about 11 yearsThe default
JTable#getColumnClass
method does this:return getModel().getColumnClass(convertColumnIndexToModel(column));
. If a user moves the table column, this "solution" will break.TableModel#getColumnClass
is what should be overridden. -
Hovercraft Full Of Eels almost 10 yearsYour code has been shamelessly copied here. I have flagged the moderators regarding that poster's actions.