JTable分拣机无法正常工作 [英] JTable sorter not working

查看:108
本文介绍了JTable分拣机无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的JTable,我希望按列升序排序。代码非常简单。但它无法正确排序行。我无法弄清楚出了什么问题。以下是我的代码:

I have a very simple JTable that I want to sort in ascending order by column 0. The code is very simple. But it is not able to sort the rows properly. I can not figure out what is wrong. Following is my code:

package test;

import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class TableSorter {

    public static void main(String[] args) {
        new TableSorter();
    }

    public TableSorter() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
              try {


       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               } catch (ClassNotFoundException | InstantiationException | 
       IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }


            DefaultTableModel model =  new DefaultTableModel(new String[] {"X", "Y", }, 0);

            model.addRow(new Object[]{5, 8});
            model.addRow(new Object[]{10, 5});
            model.addRow(new Object[]{50, 60});
            model.addRow(new Object[]{100, 60});

            JTable table = new JTable(model);
            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
            table.setRowSorter(sorter);


            List<RowSorter.SortKey> sortKeys = new ArrayList<>();
            sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
            sorter.setSortKeys(sortKeys);

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            }
        });
    }

   }

以下是我运行程序时的结果

结果:

X    y
10   5
100  60
5    8
50   60

任何帮助将不胜感激。结果附后。
提前致谢。

Any help will be appreciated. The result is attached. Thanks in advance.

推荐答案

来自 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.

这篇关于JTable分拣机无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆