简体   繁体   中英

Java jTable column listener

I have a jtable (Gui layout manager) with boolean value in one column. You will see that there will be a checkbox appearing in this column. That's all fine with me but now I would like to have a column cell event listener so I get output of the entire row data when I press this checkbox.

Now I have some success with the jtable.getModel().addTableModelListener , but this has impact on the whole table.

jTabelRooster.getModel().addTableModelListener(
new TableModelListener() {
    public void tableChanged(TableModelEvent evt) {

        String lidnummer = jTabelRooster.getValueAt(evt.getLastRow(), 0).toString();
        int parseLidnummer = Integer.parseInt(lidnummer);
        String lidVoornaam = jTabelRooster.getValueAt(evt.getLastRow(), 1).toString();
        String lidAchternaam = jTabelRooster.getValueAt(evt.getLastRow(), 3).toString();
        Boolean aanwezig = Boolean.parseBoolean(jTabelRooster.getValueAt(evt.getLastRow(), 4).toString());

    }
});

can somebody hell me in this matter?

Why don't you just override the setValueAt function of your tablemodel? It gets the two coordinates in the tablemodel, and the value the user entered

class YourTableModel extends YourBaseTableModel
{
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
        //To keep the default behavior, if you didn't write this method yourself
        super.setValueAt(aValue, rowIndex, columnIndex);
        if(columnIndex == theBooleanColumnIndex)
        {
            //now with rowIndex you can access the underlaying row
        } 
    }
}

Now I have some success with the jtable.getModel().addTableModelListener , but this has impact on the whole table.

Listen for events generated by a change in the column containing the Boolean:

public void tableChanged(TableModelEvent e)
{
    if (e.getType() == TableModelEvent.UPDATE)
    {
        int column = e.getColumn();

        if (column == ???)
        {
            ...
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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