aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Most effective JTable sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Most effective JTable sorting" Watch "Most effective JTable sorting" New topic
Author

Most effective JTable sorting

Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
Dear all,

I have a JTable that contains a large set of data (like 200000 rows). Since the sorting needs to be done on the GUI thread, it often freezes the GUI for seconds when the user tries to sort the table by a column, does anyone ever face this kind of problem before? Any suggestions ?

Thanks,
Ronnie
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Ronnie Ho:
I have a JTable that contains a large set of data (like 200000 rows). Since the sorting needs to be done on the GUI thread, it often freezes the GUI for seconds when the user tries to sort the table by a column


Why does sorting need to be done on the GUI thread? You may determine the sort order in a another thread, then set the order in the GUI thread.


bitguru blog
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Just a thought, but is it not correct that you halt the GUI while the sort happens. Presumably you don't want the user to be making changes while another thread is sorting?

Obviously it depends entirely on your implementation, but if you had:

and the sort starts, then the users changes Brian to Zebra, you'll probably get odd results.

Of course, it's probably more appropriate to display a progress bar, or similar, which would probably need you to do exactly as Brian suggested - I'm just playing devil's advocate really.
Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
Originally posted by Brian Cole:


Why does sorting need to be done on the GUI thread? You may determine the sort order in a another thread, then set the order in the GUI thread.


Thanks for the reply. How do you "set the order" in the GUI thread ? Maybe I'm missing something here. Do you mean changing the reference ?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Ronnie Ho:
How do you "set the order" in the GUI thread ? Maybe I'm missing something here. Do you mean changing the reference ?


It depends. One way is to construct a new TableModel in
the other thread then call setModel() in the GUI thread.

I don't know how well this would mesh with your existing
code. There are other approaches also.

[edit: setModel() will reset things like your column widths
and order, so you may want to look elsewhere. If your table
model inherits from AbstractTableModel you can replace the
underlying data store and call fireTableDataChanged().]
[ January 22, 2008: Message edited by: Brian Cole ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by David Payne:
Just a thought, but is it not correct that you halt the GUI while the sort happens. Presumably you don't want the user to be making changes while another thread is sorting?


I guess someone should address this, so I'll give it a try.

You definitely don't want to "halt the GUI" in the manner it
would be if you did something computationally expensive (such
as a large sort) on the GUI thread. Not only would this prevent
the user from making changes to the table (which might be a
good thing) but this would block all GUI activity whatsoever.
So windows would not be painted when unobscured, menus wouldn't
do anything when clicked, the frame's close box wouldn't work, etc.

You may indeed want to prevent the user from making changes to
the table during the sort, but tying up the GUI thread is not
the way to do it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Most effective JTable sorting