简体   繁体   English

具有复选框的Java多表模型相互干扰

[英]Java Multiple Table Models with CheckBoxes Interfering with each other

I have a function that creates a generic TabelModel that uses checkbox. 我有一个函数可以创建使用复选框的通用TabelModel。 However, when I call this method more than once, I find that CheckBoxes from both tables are interfering with each other (click on one table, the other table clicks too). 但是,当我多次调用此方法时,我发现两个表中的CheckBox相互干扰(单击一个表,另一个表也单击)。

What could be causing the references to collide with each other? 是什么导致引用相互冲突?

private TableModel<Map<String, Object>> createModel() {
    List<Map<String, Object>> list = new ArrayList<>();
    TableModel<Map<String, Object>> model = new TableModel<Map<String, Object>>( list,
                                                                                 Arrays.asList( "Name", "Enabled" ),
                                                                                 Arrays.asList( "Enabled" ), null ) {

        @Override
        public Object getValueAt( int row, int column ) {
            final Map<String, Object> object = data.get( row );
            return object.get( columnNames.get( column ) );
        }

        @Override
        public Class<?> getColumnClass( int col ) {
            if ( col == 1 ) {
                return Boolean.class;
            }
            return super.getColumnClass( col );
        }

        @Override
        public void setValueAt( Object aValue, int row, int column ) {
            final Map<String, Object> object = data.get( row );
            object.put( columnNames.get( column ), aValue );
            fireTableCellUpdated( row, column );
        }
    };
    return model;
}
JTable tableA = new JTable ( createModel());
JTable tableB = new JTable ( createModel());

Even though tableA and tableB use two different models (or do they?), when I click on a check box in tableA, tableB also reacts to it. 即使tableA和tableB使用两种不同的模型(或者它们是吗?),当我单击tableA中的复选框时,tableB也会对此做出反应。 Somehow, the checkboxes are becoming entangled. 不知何故,复选框变得纠结了。 Why does this happen? 为什么会这样?

The field 'data' was being populated with the exact same object, this case a Map. 字段“数据”中填充了完全相同的对象,本例中为“地图”。 Data is simply an object that holds whatever data this model holds. 数据只是一个对象,它包含此模型保存的所有数据。 MadProgrammer is correct that data was being shared, and the solution was make the data loading into the table seperate MadProgrammer是正确的,因为数据正在共享,解决方案是使数据加载到表中是分开的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM