Discussion:
How do I Sort multi-colum in JTable?
(too old to reply)
Tu Tran Quoc
2010-11-10 11:42:36 UTC
Permalink
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
2010-11-10 20:34:53 UTC
Permalink
Post by Tu Tran Quoc
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
2010-11-11 01:08:46 UTC
Permalink
Post by Tu Tran Quoc
Hi everyone,
In Swing we can sort JTable in one column is very easy.
http://download.oracle.com/javase/tutorial/uiswing/components/table.h...
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;
/**
  *
  */
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 );
    }
}
Woa, it worked dramatically. Thank you so much.
o***@gmail.com
2016-04-14 19:45:16 UTC
Permalink
Post by markspace
Post by Tu Tran Quoc
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;
/**
*
*/
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 );
}
}
is it possible to sort the integer using the sortKeys?

Loading...