问题
I am using DefaultTableModel as follows:
DefaultTableModel model = new DefaultTableModel (COLUMNS, 0 )
{
@Override
public boolean isCellEditable(int row, int column)
{
return (getColumnName(column).equals("Selected"));
}
public Class getColumnClass(int columnIndex)
{
if(getColumnName(columnIndex).equals("Selected"))
return Boolean.class;
return super.getColumnClass(columnIndex);
}
};
Now I want to make only one checkbox selectable in the column "Selected". How can this be done. I have tried following method also but its not working.
public void fireTableCellUpdated(int row,int column)
{
if(getColumnName(column).equals("Selected"))
{
for(int i = 0; i<getRowCount() && i!=row;i++)
setValueAt(Boolean.FALSE, row, column);
}
}
回答1:
@eatSleepCode wrote @mKorbel can you please give example code for implementing setValueAt method.
code for (OP used) DefaultTableModel,
for code based on
AbstractTableModel
is required to hold code ordering for notifierfireTableCellUpdated(rowIndex, columnIndex)
;, because/otherwise nothing will be repainted inJTables view
,there are a few important differencies betweens those two models and its notifiers, and (my view) there isn't reason to bothering with and to use
AbstractTableModel
for basic stuff (99pct of table models)
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TableRolloverDemo {
private JFrame frame = new JFrame("TableRolloverDemo");
private JTable table = new JTable();
private String[] columnNames = new String[]{"Column"};
private Object[][] data = new Object[][]{{false}, {false}, {true}, {true},
{false}, {false}, {true}, {true}, {false}, {false}, {true}, {true}};
public TableRolloverDemo() {
final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private boolean ImInLoop = false;
@Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
if (!ImInLoop) {
ImInLoop = true;
Boolean bol = (Boolean) aValue;
super.setValueAt(aValue, rowIndex, columnIndex);
for (int i = 0; i < this.getRowCount(); i++) {
if (i != rowIndex) {
super.setValueAt(!bol, i, columnIndex);
}
}
ImInLoop = false;
}
} else {
super.setValueAt(aValue, rowIndex, columnIndex);
}
}
};
table.setModel(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableRolloverDemo tableRolloverDemo = new TableRolloverDemo();
}
});
}
}
回答2:
You get an stack overflow exception because setValueAt() method triggers fireTableCellUpdated() method once again and again.
Instead, try using a table listener which would listen to check box's value change and would set all other check boxes' value to false.
回答3:
You can create your own custom cell editor that joins all check boxes in a column in a ButtonGroup
. here's how:
public class VeryComplicatedCellEditor extends DefaultCellEditor {
private ArrayList<ButtonGroup> groups;
public getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JCheckBox checkBox = new JCheckBox();
growToSize(column);
groups.get(column).add(checkBox);
return checkBox;
}
private growToSize(int size) {
groups.ensureCapacity(size);
while (groups.size() < size)
groups.add(new ButtonGroup());
}
}
There are some complications that come from the fact that we don't know how big the table is, which are mostly taken care of in the growToSize
method.
The way this works is by maintaining a list of ButtonGroup
s, one for each column. The editor component for each cell is added to the button group for its column.
来源:https://stackoverflow.com/questions/17944382/how-to-make-only-one-checkbox-selectable-in-jtable-column