Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting JTable Rows issue

Say I have a JTable with some data in it. A call to table.getValueForCell(row,col) returns the contents of the cell. This is as expected.

Now say I want to sort my table. I click the table header (column), and it sorts the table. If I make a call to table.getValueForCell(row,col) with the same values for row and col. There is now a different row here because the table was sorted. However, the table.getValueForCell(row,col) returns the old data. It seems as if the underlying data structures that hold the table data are not updated when sorting.

Any idea on how to fix this, or what I might be doing wrong?

like image 963
user489041 Avatar asked Sep 02 '25 05:09

user489041


1 Answers

The data is always stored in its original order in the TableModel. You can access the original order using:

table.getModel().getValueAt(...);

Whenever the table is sorted, only the view changes. When you get data from the table you just use:

table.getValueAt(...);

If you some reason you need to convert back and forth between the two you can use either of the approriate table methods:

convertRowIndexToModel(...); 
convertRowIndexToView(...); 
like image 200
camickr Avatar answered Sep 04 '25 20:09

camickr