• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Most effective JTable sorting

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic