• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you sort via Column name instead of Column Index?

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When opening my form with JTable the code is designed to sort on predetermined columns.
This all works great when done via column index.

What I would like to do is sort and use the actual column name instead of column index.

Is this possible? I only see setter methods which accept int values and none with String values for the actual column name.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the TableColumnModel class for a method to get the column index given the column identifier.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will have to take a closer look.
Although I was able to get the index value it seemed to be pulling it from the table instead of the model.
When there are columns hidden it throws the number off.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
convert columnIndex from view to model
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getColumnModel would work if all remained constant, but this was still part of the solution.
After further research I found that one more call is required (getColumns) which returns an Enumeration of type TableColumn.
By searching where the getIdentifier is my field I can then make the call to getModelIndex of the TableColumn object.
The model for the TableColumn will remain constant regardless of what the user face JTable looks like.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to iterate through all the TableColumns.

I suggested how you get the index of the TableColumn. Then you can use the DefaultRowSorter to sort the specified column.

If this doesn't work for you then post your SSCCE that demonstrates the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!