Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-04-14
11
Medium Priority
?
652 Views
Last Modified: 2009-07-29

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?
0
Comment
Question by:rakhare
  • 5
  • 4
  • 2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13787071
>>* 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
 

Author Comment

by:rakhare
ID: 13787107

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13787117
>>Serialize/deserialize would bring back the same TableModel.

It would bring back the last TableModel that you serialized
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rakhare
ID: 13787130

Yes, but I need to have a new TableModel with old sort orders.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13787148
Then serialize it with the old sort orders.
0
 

Author Comment

by:rakhare
ID: 13787261

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

Accepted Solution

by:
aozarov earned 1000 total points
ID: 13787638
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13790039
>>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
 

Author Comment

by:rakhare
ID: 13833556

aozarov, I will try your suggestion.
0
 

Author Comment

by:rakhare
ID: 13837413

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
 
LVL 15

Expert Comment

by:aozarov
ID: 13837451
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question