我的代码中有一个 JTable。
每当行中的任何特定列(基本上是单元格)有更新时,我都会更新该单元格中的相应图标。
所以我基本上遵循这些步骤。
第 1 步:我更新模型。
Step2 2:我在打电话
tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
这很好用。
但是当我将表头中的列从一个位置拖放到另一个位置时,问题就来了。每当对任何特定单元格进行更新时,我都会按照我之前提到的相同步骤进行操作。
问题:我没有看到绘制的图标。但是,如果我将焦点放在表格中该行的顶部,它就会绘制图标。
观察:我看到 tableRowIndex 和 tableColumnIndex 在拖动列后是正确的。
只是为了测试,我在问题场景中添加了这段代码。
examTable.repaint(examTable.getCellRect(examTableRowIndex, examTableColumnIndex, true));
这是正确地重新绘制单元格。
但我猜这不是正确的解决方案。我尝试调试我没有发现太多关于问题的代码
最佳答案
I'm calling tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
这是错误的。你不应该直接调用 firXXX 方法。这是 TableModel 的工作,在数据更改时调用适当的事件。
I will update the corresponding icon in that cell.
您需要做的就是调用 model.setValueAt(...) 方法来更改图标,模型将通知表格数据已更改,以便表格可以重新绘制自身。
examTable.repaint(...)
同样,您不需要在表上手动调用重绘
But problem comes when I drag and drop the columns from one position to another in Table header.
如果您遵循上面的建议,不确定为什么需要特殊代码。但是,如果出于某种原因它仍然是必要的,那么您需要查看 convertColumnIndex...(...)
方法以确保您为您的列使用了正确的索引。
关于java - TableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex) 没有更新 JTable 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25424027/