简体   繁体   English

Jtable中的更新复选框进入递归状态,导致堆栈溢出错误

[英]Updating checkboxes in Jtable goes into recursion, causes stack overflow error

I have a Jtable set up in a tabbed pane that is filled with an id and checkboxes. 我在一个带ID和复选框的选项卡式窗格中设置了Jtable。 The table looks something along the lines of this, where . 表格看起来与此类似,其中. are the checkboxes. 是复选框。

         |       VAR1        |
ID | ALL | subVar1 | subVar2 |
------------------------------
id1|  .  |    .    |    .    |

Now, I also have a TableListener attached to this table. 现在,我还将一个TableListener附加到该表。 What I would like to happen is that, whenever a user presses the ALL checkbox, all the checkboxes in that row need to be selected (ie true). 我想发生的事情是,每当用户按下ALL复选框时,都需要选中该行中的所有复选框(即true)。 This is the table listener code. 这是表侦听器代码。

@Override
public void tableChanged(TableModelEvent e) {
    if(e.getSource() == assignedTableModel) {
        for (int i = 0; i < aTable.getRowCount(); i++) {
            boolean isAllChecked = (Boolean) aTable.getValueAt(i, 1);
            if(isAllChecked) {
                assignedTableModel.setValueAt(Boolean.TRUE, i, j);
            }
            else {
            ...
            }
        }
    }
}

Clicking the ALL checkbox causing the table to change, assignedTableModel.setValueAt(Boolean.TRUE, i, j); 单击ALL复选框,使表发生更改, assignedTableModel.setValueAt(Boolean.TRUE, i, j); is called, the table is changed again and therefore calls the listener, which calls this function again. 调用后,表将再次更改,因此将调用侦听器,侦听器将再次调用此函数。

My question, is there another way of updating the checkboxes? 我的问题是,还有另一种更新复选框的方法吗? Or is there a way to set a base to get out of the recursion? 还是有办法设置基础以摆脱递归?

EDIT The rows are added dynamically. 编辑行是动态添加的。 I'm wondering if adding an actionListener to the ALL checkbox as it's being added will be a solution. 我想知道是否将actionListener添加到ALL复选框中,这将是一个解决方案。 I'll come back with how it turns out. 我将返回结果。

EDIT2 I'd forgot to mention that the whole table is generated dynamically. EDIT2我忘了提及整个表是动态生成的。 That means I have no way of knowing how many columns and rows will be present, the only columns I know are ID and the ALL col. 这意味着我无法知道将出现多少列和行,我只知道ID和ALL列。 Most answers already present deal with hard coded implementations. 已经给出的大多数答案都与硬编码实现有关。

whenever a user presses the ALL checkbox, all the checkboxes in that row are selected 每当用户按下ALL复选框时,该行中的所有复选框都会被选中

So why are you looping through all the rows in your code? 那么,为什么要遍历代码中的所有行呢? The event will be generated only for the row you click and you only need to select the check marks for the columns on that row. 该事件仅针对您单击的行生成,并且您只需要为该行的列选择复选标记。 Get rid of the looping code. 摆脱循环代码。

the table is changed again and therefore calls the listener, which calls this function again. 该表将再次更改,因此将调用侦听器,该侦听器将再次调用此函数。

You need an if condition to identify when the check box in the first column is checked: 您需要一个if条件来标识何时选中第一列中的复选框:

if (e.getType() == TableModelEvent.UPDATE)
{
    int row = e.getFirstRow();
    int column = e.getColumn();

    if (column == 0)
    {
        TableModel model = (TableModel)e.getSource();
        model.setValueAt(Boolean.true, row, 1);
                ...
    }
}

Now the change of state on the other columns will be ignored. 现在,其他列的状态更改将被忽略。

Based off of @camickr's response, the following piece of code seems to be doing the trick. 基于@camickr的响应,以下代码似乎可以解决问题。 Thanks. 谢谢。

    @Override
    public void tableChanged(TableModelEvent e) {
        if (e.getType() == TableModelEvent.UPDATE && e.getSource() == assignedTableModel)
        {
            int row = e.getFirstRow();
            int column = e.getColumn();

            if (column == 1)
            {
                DefaultTableModel model = (DefaultTableModel) e.getSource();
                for(int i=2 ; i<model.getColumnCount() ; i++) {
                    if((boolean) model.getValueAt(row, 1))
                        model.setValueAt(Boolean.TRUE, row, i);
                    else
                        model.setValueAt(Boolean.FALSE, row, i);
                }
            }
        }
}

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

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