Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jTable sort question

Status
Not open for further replies.

tvrtko

Programmer
Nov 26, 2003
53
HR
How to sort data in jTable?

Thanks in advance.
 
How are you holding the data for the table? Is there a chance you could pre-sort it before building the table? You can use the java.util.Collections.sort() method to sort collections.

A better way is to hold the table data in a TableModel derivation. Have a look at the javax.swing.table.TableModel and javax.swing.table.AbstractTableModel classes. Then subclass AbstractTableModel to hold your data and set this on the JTbale using the setModel method. If you then add a sort() method to your table model which sorts the underlying data it holds, then calls the fireTableDataChanged() method of the super class, then calls to this sort method will magically sort the whole table.

You can add your own add/remove methods to your table model which allow you to add/remove rows from the underlying data which reflects in the visible JTable automatically.

Hope this all makes sense.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top