[英]JTable remove row by clicking a cell with a custom DeleteCellEditor from the same JTable
在一个名为HistoryPanel
的类中,我有一个名为resultsTable
的静态JTable,它带有一个名为tableModel
的静态DefaultTableModel。 该表有一个自定义单元格编辑器:
resultsTable.getColumn("Delete").setCellEditor(new DeleteButtonEditor(new JCheckBox()));
在内部,当然,我重写这两个方法,如下所示:
public Object getCellEditorValue()
{
if (isPushed)
{
HistoryPanel.tableModel.removeRow(HistoryPanel.resultsTable.getSelectedRow());
}
}
protected void fireEditingStopped()
{
super.fireEditingStopped();
}
仅当我尝试删除resultsTable的最后一行时才会抛出异常。 当我删除不是最后一行时,它完美地工作。 例外是:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
导致异常的代码行是:
super.fireEditingStopped();
谁能帮我? 我怎样才能避免这种异常
目前,您在编辑操作期间删除了正在编辑的行。 如果您发布了SSCCE,那么我可以更轻松地测试我提出的解决方案,但现在我会将其留给您。
我认为包装你的
if (isPushed)
{
HistoryPanel.tableModel.removeRow(HistoryPanel.resultsTable.getSelectedRow());
}
在EventQueue.invokeLater
调用内部可能会解决问题。 这样,删除行时编辑就完成了。
真的为什么不直接使用Boolean
值,为什么有Editor for JCheckBox
,你必须检查教程如何工作JTable
编辑
对于真正的TableCellEditor
你必须调用
int row = table.convertRowIndexToModel(table.getEditingRow());
fireEditingStopped();
然后你可以打电话删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.