lundi 22 février 2016

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. 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. What I would like to happen is that, whenever a user presses the ALL checkbox, all the checkboxes in that row are selected (ie 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); 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. I'll come back with how it turns out.




Aucun commentaire:

Enregistrer un commentaire