TableCellEditor中的JComboBox在不同的行甚至不同的TableModels中记住最后选择的值。例如,在一行上选择一个值,然后转到另一行,开始编辑单元格,JComboBox将把上一行上的最后一个选择值作为其当前值。
它怎么修复呢?
发布于 2011-08-24 21:28:31
在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);
}https://stackoverflow.com/questions/7176073
复制相似问题