简体   繁体   English

Java jTable列侦听器

[英]Java jTable column listener

I have a jtable (Gui layout manager) with boolean value in one column. 我有一个jtable (Gui布局管理器),其中一列具有boolean值。 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. 现在,我使用jtable.getModel().addTableModelListener获得了一些成功,但这会影响整个表。

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? 为什么不覆盖表格模型的setValueAt函数呢? 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. 现在,我使用jtable.getModel()。addTableModelListener获得了一些成功,但这会影响整个表。

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 == ???)
        {
            ...
        }
    }
}

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

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