Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I Sort multi-colum in JTable?

2,370 views
Skip to first unread message

Tu Tran Quoc

unread,
Nov 10, 2010, 6:42:36 AM11/10/10
to
Hi everyone,

In Swing we can sort JTable in one column is very easy.
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting

How can I create a multi-column sort in JTable? Should I inherit from
DefaultRowSorter or RowSorter or have a good way for this???

Thanks

markspace

unread,
Nov 10, 2010, 3:34:53 PM11/10/10
to
On 11/10/2010 3:42 AM, Tu Tran Quoc wrote:
> Hi everyone,
>
> In Swing we can sort JTable in one column is very easy.
> http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting


First have a look at SortKeys. Those are explained in the link you
posted, but just a bit further down the page.

This does allow you to sort by multiple columns. However, it seems to
only work once for the default sort. Once a user starts clicking on
columns, I don't see a way to re-establish the default sort.

Note in this example the 2nd table is sorted by the 2nd column first,
then by the 1st column.

package test;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter.SortKey;
import javax.swing.SortOrder;
import javax.swing.SwingUtilities;

/**
*
* @author Brenden
*/
public class TableSorter {

public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGui();
}
} );
}

private static void createAndShowGui()
{
JFrame jf = new JFrame("Table Sorter");
Box box = Box.createVerticalBox();

JTable jt1 = makeNewJTable();
jt1.setAutoCreateRowSorter( true );
JScrollPane sc1 = new JScrollPane( jt1 );
sc1.setPreferredSize( new Dimension( 400, 150 ) );
box.add( sc1 );

box.add( Box.createRigidArea( new Dimension( 20, 20 ) ) );

JTable jt2 = makeNewJTable();
jt2.setAutoCreateRowSorter( true );
List<SortKey> sortKeys = new ArrayList<SortKey>();
sortKeys.add( new SortKey( 1, SortOrder.ASCENDING ) );
sortKeys.add( new SortKey( 0, SortOrder.ASCENDING ) );
jt2.getRowSorter().setSortKeys( sortKeys );
JScrollPane sc2 = new JScrollPane( jt2 );
sc2.setPreferredSize( new Dimension( 400, 150 ) );
box.add( sc2 );

jf.add( box );
jf.pack();
jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jf.setLocationRelativeTo( null );
jf.setVisible( true );

}

private static final Object[][] tableData =
{
{ "Lisa", "Simpson", "Boxing", 2, true},
{ "Bart", "Simpson", "Checkers", 1, false},
{ "Joe", "Abbey", "Hockey", 10, false},
{ "Abigale", "Abbey", "Skating", 5, true },
};
private static final String[] columnNames =
{ "First Name", "Last Name", "Sport", "Years", "Vegitarian" };

private static JTable makeNewJTable()
{
return new JTable( tableData, columnNames );
}
}


Tu Tran Quoc

unread,
Nov 10, 2010, 8:08:46 PM11/10/10
to
On Nov 11, 3:34 am, markspace <nos...@nowhere.com> wrote:
> On 11/10/2010 3:42 AM, Tu Tran Quoc wrote:
>
> > Hi everyone,
>
> > In Swing we can sort JTable in one column is very easy.
> >http://download.oracle.com/javase/tutorial/uiswing/components/table.h...

Woa, it worked dramatically. Thank you so much.

op.m...@gmail.com

unread,
Apr 14, 2016, 3:45:20 PM4/14/16
to
is it possible to sort the integer using the sortKeys?
0 new messages