开发者

Swing JTable reset TableCellEditor

JComboBox in TableCellEditor remember last selected开发者_如何转开发 value among different rows and even different TableModels. For example select a value on one row, then go to another row, start cell editing and JComboBox will have as its current value last select value on the previos row.

How can it fixed?


Set the value in the getTableCellEditorComponent(..) method.

Example:

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜