Search code examples
javaswingjtablejcheckbox

Listeneing for changes in JCheckBox in a JTable


How do I listen for changes in a JTable column that has a JCheckBox in it? I want to know when the user selects/deselects the check box. The class for the column is set as boolean so it is automatically rendered as a JCheckBox.


Solution

  • Thanks to the comment by mKorbel, I re-wrote the setValueAt method for the table model as such:

    public void setValueAt(Object value, int row, int col) {
        super.setValueAt(value, row, col);
        if (col == 4) {
            if ((Boolean) this.getValueAt(row, col) == true) {
                //code goes here
            }
            else if ((Boolean) this.getValueAt(row, col) == false) {
                //code goes here
            }
        }   
    }
    

    Now the code is only executed when the value in the cell actually changes, which is what I want.