How to remember TableSorter's sorted settings (for JTable) and recreate them?


I am using the TableSorter listed below succesfully to use a sorted JTable.

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting

My application needs to do the following:

* Capture that sorted settings when the table is sorted.
* Later when I recreate the table, apply these to the new sorted JTable.

How can I do this?
rakhareAsked:
Who is Participating?
 
aozarovCommented:
You just need to save the column number and the status of the one which is sorted.
You can do that by looping from 0 to the number of columns - 1 and call the TableSorter getSortingStatus(column).
Save the column number and the status of the one which is not NOT_SORTED.
Then after  you create a new TableSorter with fresh Model you can call the table setSortingStatus(with_the_saved_column_number,
and_the_saved_status); to re-apply the previous sort.
0
 
CEHJCommented:
>>* Capture that sorted settings when the table is sorted.

That's already done - by the TableModel

>>* Later when I recreate the table, apply these to the new sorted JTable.

Not clear on what you mean by this, but if you mean restore the sorted version, just serialize the TableModel to save and deserialize to load:

http://javaalmanac.com/egs/java.io/SerializeObj.html

0
 
rakhareAuthor Commented:

Serialize/deserialize would bring back the same TableModel. But my TableModel might have changed when it is recreated. Imagine this like a Hotmail email box which you can sort and Hotmail remembers the sort orders. Also, in my case some rows mays may be deleted/updated because of database triggers too, so I need to use a fresh TableModel, but use the same TableSorter.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
CEHJCommented:
>>Serialize/deserialize would bring back the same TableModel.

It would bring back the last TableModel that you serialized
0
 
rakhareAuthor Commented:

Yes, but I need to have a new TableModel with old sort orders.
0
 
CEHJCommented:
Then serialize it with the old sort orders.
0
 
rakhareAuthor Commented:

Since it is a database based application, I would like to store and restore the sorting with values instead of serialized objects.
0
 
CEHJCommented:
>>Since it is a database based application ...

That doesn't matter - you can just save the whole model as one value in the db
0
 
rakhareAuthor Commented:

aozarov, I will try your suggestion.
0
 
rakhareAuthor Commented:

CEHJ's solution is technically correct, but I think aozarov's suggestion is better. More cleaner, extensible and reusable. For eg. if the sorting order is saved as numbers, any other UI, say a VB interface getting the data from the DB can display it as well.
0
 
aozarovCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.