We are using a JTable
which displays data along with Status (New
, Processed
, Closed
). Each status row has a different color which is achieved by overloading prepareRenderer()
of JTable
.
Now we need to sort that table and we are using table.setAutoCreateRowSorter(true);
to achieve that. The rows get sorted properly, but the color of rows remains the same. We need to reapply the color to all the rows after this operation based on the status column.
I was wondering what could be the best way to achieve that. There are several ways I can think of:
- Repaint/Revalidate the table. But simply doing this would not work I think.
- Capture
mouseClicked
event and identify whether column header was clicked then callprepareRenderer()
manually and then call repaint/revalidate - Then I read one of the questions here wherein one of the answers was mentioned not to call repaint/revalidate directly, rather change the underlying data model and it will automatically call the above methods.
I don't know how to go about it. Can anyone please provide an insight into what is the correct way to achieve this?
For change cell color in
JTable
withsetAutoCreateRowSorter(true)
I used method table.getRowSorter().convertRowIndexToModel(row) in myTableCellRenderer
And then