我开始输入我的代码:
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());
}
我希望我的 graphicTable
通过给它一个自定义的 CellEditorListener
来检测其单元格中的数据更改,但我真的不明白如何添加它。我尝试了几次,代码如下:
DefaultCellEditor editor = new DefaultCellEditor(new JTextLabel());
editor.addCellEditorListener(new MyCellEditorListener());
this.graphicTable.setCellEditor(editor);
...或:
this.graphicTable.setCellEditor(this.graphicTable.getCellEditor().addCellEditorListener(new MyCellEditorListener()));
...然而,这些技术在这两种情况下都会给我一个 NullPointerException
。
我通过论坛四处寻找解决方案,但它们只是让我更加困惑。
每一个提示都将不胜感激。
提前致谢。
最佳答案
你的方法是不正确的。 您可以轻松检测 TableModel
中的数据更改,特别是在 setValueAt
方法中。一旦检测到更改并对其使用react,您必须调用 fireTable..
方法之一,让表和所有其他监听器知道数据已更改
根本不需要为单元格编辑器分配任何监听器。
关于java - 如何将自定义的 CellEditorListener 添加到我的 JTable 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18001171/