Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4747079

Add sorting ability to JTable

XMLWordPrintable

    • b39
    • generic, x86, sparc
    • generic, linux, solaris_7, windows_2000, windows_xp

      This was pulled from 4267065:

      Name: krT82822 Date: 08/31/99


      It would be great if Swing included the TableSorter classes
      published in the Swing Connection as part of the official
      Swing release. It would avoid a lot of code duplication since
      I bet 95% of people writing JTables need to be able to sort
      them. Furthermore, it should include icons to visually notify
      the user by which column the table is currently sorted, etc.

      Yes, I know it can be done on top of Swing (and that only
      shows that Swing has been very well designed), but it is such
      a common feature that IMO it should be a part of Swing.

      Thanks in advance,

      Hernan
      (Review ID: 94476)
      ======================================================================

      Another customer comments:
      I just read where sorting will be restored to JTable in Tiger.
      I would like to suggest that sorting by an invisible column be allowed.
      We sometimes have a column in the model, that is calculated from several
      of the other columns, that is the default sort order for the rows.
      When clicking on the column header we go thru 3 states for the sort:
      ascending, descending, & default column.
      If the table doesn't have an invisible default sort column, then it just
      has ascending & descending.

      =============================================================================
      ###@###.### 2004-01-05

      Same request from a CAP member and suggested methods:

      suggest this SortableTableModel class

      If set SortableTableModel to a JTable
      then this JTable get sorting ability.

      If click a header, the whole rows of JTable are sorted
      acoording to the column which is clicked.

      The attached file FileTable.zip has 3 files :

      SortableTableModel.java
        TbleModel which can sort JTable data

      FileTable.java
        the example to use SortableTableMode

      FileTableTest.java
        test driver which has main method for FileTable and also SortableTableModel.

      ------------------------------------------------------------------------------
      The following is a flagment of FileTable which shows how to use SortableTableModel

      class FileTable extends JTable {

         DefaultTableModel model;
         String dirName;

         FileTable( String dirName ){
            super();
            if( dirName==null ) dirName = ".";

            model = new SortableTableModel( this, columnNames, 0 );
            setModel( model ); // just set SortableTableModel

            setRowSelectionAllowed(false); // this is important
            //.......

      -----------------------------------------------------------------------------
      The the following is the code of SortableTableModel.java
      //------------------------------ SortableTableModel.java
      import javax.swing.*;
      import javax.swing.table.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;

      public class SortableTableModel extends DefaultTableModel {
        JTable table;
        JTableHeader header;
        Vector columnNames;
        int selectedHeaderColumn;
        int columnCount;
        int[] minWidth;
        TableCellRenderer[] cellRenderer;
        int clickCount = 0;

        public SortableTableModel( JTable table, String[] columnNames, int rowCount ){
          super( columnNames, rowCount );
          this.table = table;
          header = table.getTableHeader();
          header.setReorderingAllowed(false);
          header.addMouseListener( new HeaderMouseAdapter() );
          this.columnNames = DefaultTableModel.convertToVector( columnNames );
          columnCount = columnNames.length;
          minWidth = new int[ columnCount ];
          cellRenderer = new TableCellRenderer[ columnCount ];
        }

        class HeaderMouseAdapter extends MouseAdapter {
          public void mousePressed( MouseEvent e ){
            Point mousePoint = new Point( e.getX(), e.getY() );
            selectedHeaderColumn = header.columnAtPoint( mousePoint );
            DefaultTableColumnModel cmodel = (DefaultTableColumnModel)table.getColumnModel();
            backupTableColumnAttr( cmodel );

            clickCount++;
            boolean reverseOrder = false;
            if( ( clickCount %2 )==0 ) reverseOrder = true;
            sort( reverseOrder );

            restoreTableColumnAttr( cmodel );
          }
          void backupTableColumnAttr( DefaultTableColumnModel cmodel ){
            for( int i=0 ; i< columnCount ; i++ ){
              TableColumn column = cmodel.getColumn(i);
              minWidth[i] = column.getMinWidth();
              cellRenderer[i] = column.getCellRenderer();
            }
          }
          void restoreTableColumnAttr( DefaultTableColumnModel cmodel ){
            for( int i=0 ; i< columnCount ; i++ ){
              TableColumn column = cmodel.getColumn(i);
              column.setMinWidth( minWidth[i] );
              column.setCellRenderer( cellRenderer[i] );
            }
          }
        }

        void sort( boolean reverseOrder ){
          Vector vec = getDataVector();
          Object[] array = vec.toArray();
          Arrays.sort( array, new Compare( selectedHeaderColumn, reverseOrder ) );
          vec = DefaultTableModel.convertToVector( array );
          setDataVector( vec, columnNames );
        }

        class Compare implements Comparator {
          int sortKey;
          boolean reverseOrder;
          Compare( int sortKey, boolean reverseOrder ){
            this.sortKey = sortKey;
            this.reverseOrder = reverseOrder;
          }
          public int compare( Object o1, Object o2 ){
            Vector v1 = (Vector)o1;
            Vector v2 = (Vector)o2;
            Comparable s1 = (Comparable)v1.get( sortKey );
            Comparable s2 = (Comparable)v2.get( sortKey );
            int ans = s1.compareTo( s2 );
            if( reverseOrder ) ans *= -1;
            return( ans );
          }
          public boolean equals( Object obj ){
            return( false );
          }

        }
      }
      //------------------------------------------------------------------------------

            svioletsunw Scott Violet (Inactive)
            svioletsunw Scott Violet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: