且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JTable分拣机无法正常工作

更新时间:2023-12-03 14:03:10

来自 DefaultTableModel


警告:DefaultTableModel返回Object的列类。当
DefaultTableModel与TableRowSorter一起使用时,这将导致
广泛使用toString,对于非String数据类型,这是昂贵的
。如果你使用带有TableRowSorter的DefaultTableModel,你强烈建议你使用
覆盖getColumnClass以返回
适当的类型。

Warning: DefaultTableModel returns a column class of Object. When DefaultTableModel is used with a TableRowSorter this will result in extensive use of toString, which for non-String data types is expensive. If you use DefaultTableModel with a TableRowSorter you are strongly encouraged to override getColumnClass to return the appropriate type.

您需要覆盖表模型的getColumnClass,如:

You need to override getColumnClass of your table model like :

DefaultTableModel model =  new DefaultTableModel(new String[] {"X", "Y", }, 0)
{
        @Override
        public Class<?> getColumnClass(int column) 
        {
              Class<?> returnValue;
              if ((column >= 0) && (column < getColumnCount())) 
              {
                  returnValue = getValueAt(0, column).getClass();
              } 
              else 
              {
                 returnValue = Object.class;
              }

              return returnValue;

       };
    };

在你的情况下它是比较 Integer.toString()关于整数,因此你看到错误的顺序。
通过重写 getColumnClass()返回 Integer 类型,您将获得整数值的比较。

In your case it is comparing Integer.toString() on integers and hence the wrong order that you see. By overriding getColumnClass() to return Integer type, you will get comparison of integers by their values.