I start typing my code:

private void addMyCellEditorListener() {

    class MyCellEditorListener implements CellEditorListener
    {
        public MyCellEditorListener() {}

        public void editingCanceled(ChangeEvent e) {}

        public void editingStopped(ChangeEvent e) {
            if(row == 0 && column > 0)
                rechargeTableWithFindedResults(graphicTable.getValueAt(row,column));
            else
                dataTable.setValueAt(graphicTable.getValueAt(row,column),row,column);
        }
    };

    .... addCellEditorListener(new MyCellEditorListener());
}

I would like my graphicTable to detect data changes into its cells by giving it a customized CellEditorListener, but I really can't understand how to add it. I tried several times with a code like the following:

DefaultCellEditor editor = new DefaultCellEditor(new JTextLabel());
editor.addCellEditorListener(new MyCellEditorListener());
this.graphicTable.setCellEditor(editor);

... or:

this.graphicTable.setCellEditor(this.graphicTable.getCellEditor().addCellEditorListener(new MyCellEditorListener()));

... however these techniques give me a NullPointerException in both cases.

I have looked around through forums to get a solution, but they are just getting me more confused.

Every hint would be appreciated.

Thanks in advance.

有帮助吗?

解决方案

Your approach is incorrect. You can easily detect data changes in your TableModel, specifically in setValueAt method. Once you detected the change and reacted on it, you have to call one of the fireTable.. methods to let table and all other listeners know that data changed

There no need to assign any listeners to cell editors at all.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top