简体   繁体   中英

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

I have a JTable in my code.

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.

Step2 2: I'm calling

       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.

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);

That is wrong. You should never invoke the firXXX methods directly. That is the job of the TableModel to invoke the appropriate event when the data is changed.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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