Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable: How to keep selected row visible after sort?  RSS feed

 
Bottolf Knotten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to keep the selected row visible whenever a JTable is sorted. I've tried to fix it in a RowSorterListener, but it seems like the listener is called BEFORE the table is sorted (table.getSelectedRow() returns the row which was selected before the sort). The attached example will hopefully show what I mean. If I select a row and then sort the table by clicking one of the column headers, the selected row will most likely disappear out of view.

Any suggestions?

Thanks.

 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a very good example since the data in the model keep changing...

Anyway, I think the problem is that the data has actually been sorted when the RowSorterEvent is fired. So try wrapping the code in an invokelater to add your selection code to the end of the EDT so that the scrollRectToVisible() is done after the sorting is finished.

 
Bottolf Knotten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!