Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable Multi Column Sort selected row not correct

 
Jay Brass
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey There,

I have a JTable in a JScrollPane. The table model is created from a resultset from a database query. I've set tempTable.setRowSorter(sorter) where sorter is a new TableRowSorter<TableModel>(model); I've also set sorter.setMaxSortKeys(4); because I only want to sort on 4 specific columns. Currently I call a header render to sort the columns but after it sorts them the selected row is incorrect. I had to add the columns to the SortKeys and adjust for the column selected to sort on so that it would be similar to an OrderBy clause in a database query. Does anyone have any idea how to keep the selected column after the sort? Converting to model or view don't seem to help.
From the class where the table is:


the table header render class
 
Jay Brass
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized that my rowListener and my tableHeaderRender were in both trying to update the selected row. I created a RowClickedListener that extended MouseAdapter to check for when a user clicked on a row instead of when they clicked on a header. Long story short, this allowed me to set a boolean value and only change (or reset) the selected row to what the user clicked on and not what the header sort had changed it to. Seems to work pretty well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic