JDK-4747079 : Add sorting ability to JTable
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0,5.0,6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    generic,linux,solaris_7,windows_2000,windows_xp generic,linux,solaris_7,windows_2000,windows_xp
  • CPU: generic,x86,sparc
  • Submitted: 2002-09-13
  • Updated: 2017-05-16
  • Resolved: 2005-06-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
6 b39Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
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,

(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 :

  TbleModel which can sort JTable data

  the example to use SortableTableMode

  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 ){
      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.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 );

      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 );


EVALUATION JTable now offers the ability to sort and filter at the row level. This resulted in introducing the following API: TableStringConverter: This is responsible for converting Object values to Strings. As of this feature sorting is the only one that can make use of this class. This class allows you to use a separate class that is responsible for converting the objects to string when comparing. You do not need this to use sorting, it's only useful if you don't want to overload getValueAt to convert to Strings. RowFilter: Adds the ability to filter out rows from the view. Has the single method include as well as various static methods to get instances meeting certain criteria (like numbers, strings, dates...) SortOrder: Enum of ascending/descending/unsorted RowSorter: Object responsible for mapping coordinates between the table model and JTable. RowSorter also has it's own listener, a List of sort orders and methods that are called when the model changes. This class is not specific to JTable. DefaultRowSorter: Default implementation of RowSorter. This does not use TableModel, rather it's own Model class. Has methods for setting Comparators per column, whether or not a column is sortable, max number of sort keys ... TableRowSorter: TableModel specific implementation of RowSorter. It extends DefaultRowSorter providing it's own DefaultRowSorter.Model implementation that wraps TableModel. And finally JTable has methods for setting the RowSorter, mapping coordinates and listeners to sorter changes. Here's what you'll do to turn on sorting: TableModel myModel = createTableModel(); JTable table = new JTable(myModel); table.setRowSorter(new TableRowSorter(myModel)); To handle mapping of indices you'll need to do: int[] selection = table.getSelectedRows(); for (int i = 0; i < selection.length; i++) { selection[i] = table.convertRowIndexToModel(selection[i]); } ###@###.### 2005-05-25 22:30:38 GMT

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang