Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #3825
| Newsgroups | comp.lang.java.help |
|---|---|
| Date | 2016-04-14 12:45 -0700 |
| References | <a29e31af-bf21-4dad-9b51-a75ee28b3396@n10g2000prj.googlegroups.com> <ibevle$a13$1@news.eternal-september.org> |
| Message-ID | <5e0529a0-6fab-4d63-9bfa-9ebb03be86c1@googlegroups.com> (permalink) |
| Subject | Re: How do I Sort multi-colum in JTable? |
| From | op.mrafiq@gmail.com |
On Wednesday, 10 November 2010 20:34:53 UTC, markspace 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.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 );
> }
> }
is it possible to sort the integer using the sortKeys?
Back to comp.lang.java.help | Previous | Next | Find similar
Re: How do I Sort multi-colum in JTable? op.mrafiq@gmail.com - 2016-04-14 12:45 -0700
csiph-web