简体   繁体   English

TableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex) 不更新 JTable 中的单元格

[英]TableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex) is not updating the cell in JTable

I have a JTable in my code.我的代码中有一个 JTable。

And whenever there is an update to any specific column in the row (cell basically), I will update the corresponding icon in that cell.每当行(基本上是单元格)中的任何特定列有更新时,我都会更新该单元格中的相应图标。

so I'm basically following these steps.所以我基本上遵循这些步骤。

Step 1: I update the model.第 1 步:我更新模型。

Step2 2: I'm calling第2步2:我打电话

       tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);

This works fine.这工作正常。

But problem comes when I drag and drop the columns from one position to another in Table header.但是当我将列从表标题中的一个位置拖放到另一个位置时,问题就出现了。 And whenever there is an update to any specific cell, I follow the same steps as I mentioned before.每当对任何特定单元格进行更新时,我都会按照我之前提到的相同步骤进行操作。

Problem : I'm not seeing the Icon painted.问题:我没有看到图标被绘制。 But if I bring the focus on top of that row in table it is painting the icon.但是,如果我将焦点放在表格中该行的顶部,则它正在绘制图标。

Observation : I see the tableRowIndex and tableColumnIndex are correct after dragging the columns.观察:拖动列后,我看到 tableRowIndex 和 tableColumnIndex 是正确的。

Just for testing I added this piece of code in the problem scenario.只是为了测试,我在问题场景中添加了这段代码。

     examTable.repaint(examTable.getCellRect(examTableRowIndex, examTableColumnIndex, true));

This is repainting the cell properly.这是正确重新绘制单元格。

But this is not the right solution I guess.但这不是我猜的正确解决方案。 I tried to debug the code I didn't find much about the problem我试图调试代码我没有找到太多关于问题的信息

I'm calling tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);我打电话给 tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);

That is wrong.那是错的。 You should never invoke the firXXX methods directly.永远不要直接调用 firXXX 方法。 That is the job of the TableModel to invoke the appropriate event when the data is changed.这是 TableModel 的工作,用于在数据更改时调用适当的事件。

I will update the corresponding icon in that cell.我将更新该单元格中的相应图标。

All you need to do is invoke model.setValueAt(...) method to change the Icon and the model will notify the table that data has changed so the table can repaint itself.您需要做的就是调用 model.setValueAt(...) 方法来更改图标,模型将通知表数据已更改,以便表可以重新绘制自身。

examTable.repaint(...) ExamTable.repaint(...)

Again you should not need to manually invoke repaint on the table同样,您不需要手动调用桌子上的重绘

But problem comes when I drag and drop the columns from one position to another in Table header.但是当我将列从表标题中的一个位置拖放到另一个位置时,问题就出现了。

Not sure why you need special code for this if you follow the advice from above.如果您遵循上面的建议,不确定为什么需要特殊代码。 But if for some reason it is still necessary then you need to look at the convertColumnIndex...(...) methods to make sure you are using the proper index for your column.但是如果由于某种原因仍然有必要,那么您需要查看convertColumnIndex...(...)方法以确保您为您的列使用了正确的索引。

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

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