JComboBox
在TableCellEditor
记住不同行甚至不同的最后选择的值TableModels
。例如,在一行上选择一个值,然后转到另一行,开始单元格编辑,JComboBox
并将在前一行上的最后一个选择值作为其当前值。
怎么能修好?
JComboBox
在TableCellEditor
记住不同行甚至不同的最后选择的值TableModels
。例如,在一行上选择一个值,然后转到另一行,开始单元格编辑,JComboBox
并将在前一行上的最后一个选择值作为其当前值。
怎么能修好?
getTableCellEditorComponent(..)
在方法中设置值。
例子:
public static void main(String... args) {
JFrame frame = new JFrame("Test");
JTable table = new JTable(10, 2);
JComboBox box = new JComboBox(new String[] {"A", "B", "C"});
table.setDefaultEditor(Object.class, new DefaultCellEditor(box) {
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
return super.getTableCellEditorComponent(
table,
table.getValueAt(Math.max(row-1, 0), column),
isSelected,
row,
column);
}
});
frame.add(table);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}