JXTable column sorting changes between 1.0 and 1.6

走远了吗. 提交于 2019-12-10 21:07:31

问题


I recently updated the SwingX library in an application from version 1.0 to 1.6.2 since we updated to JDK1.6 . I know the sorting has been changed to re-use some of the Core JDK components which were introduced in JDK 1.6 .

However, in version 1.0 it was possible to sort a column by clicking on the header, subsequent clicks reverted the sort order, and shift click removed the sorting and reverted back to the original order. After the update to version 1.6.2, the shift click behavior is no longer present.

A small sample

import org.jdesktop.swingx.JXTable;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import java.awt.EventQueue;

public class JXTableSortingTest {
  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      public void run() {
        initUI();
      }
    } );
  }

  private static void initUI(){
    JFrame testFrame = new JFrame( "TestFrame" );
    JXTable table = new JXTable(  );
    DefaultTableModel model =
        new DefaultTableModel( new Object[][]{ new Object[]{"A"}, new Object[]{"B"}, new Object[]{"C"}, new Object[]{"D"}, new Object[]{"E"} }, new Object[]{"Click me"} );
    table.setModel( model );

    testFrame.getContentPane().add( new JScrollPane( table ) );

    testFrame.pack();
    testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    testFrame.setVisible( true );
  }
}

Running this sample with version 1.0 allows to remove the column sorting with a shift-click on the header, and with the 1.6.2 version this no longer works.

I checked the docs but did not encounter anything to switch this behavior back on. So before I start adding this functionality I though I asked it here if anybody knows an easy way to re-introduce the shift-click behavior


回答1:


it's not supported, at least not in the exact same way as in 1.0.

The nearest you can come with default support is to set the sortOrderCycle property on the JTable, then repeated clicks will cycle through those states for the column which is clicked:

table.setSortOrderCycle(ASCENDING, DESCENDING, UNSORTED);

This differs from the old behaviour in that the old removed all sorts - if you need that, a custom mouseListener which resets the sort keys is the way to go.

Plus you might consider filing a feature request in the SwingX issue tracker and start a discussion over on the SwingLabs forum: the main reason we decided to drop it, was consistency with core behaviour. And as nobody barked, it was left at that :-)



来源:https://stackoverflow.com/questions/9532044/jxtable-column-sorting-changes-between-1-0-and-1-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!