0

JComboBoxTableCellEditor记住不同行甚至不同的最后选择的值TableModels。例如,在一行上选择一个值,然后转到另一行,开始单元格编辑,JComboBox并将在前一行上的最后一个选择值作为其当前值。

怎么能修好?

4

1 回答 1

3

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);
}
于 2011-08-24T13:28:31.237 回答