java – 为什么我的JTable不正确地排序整数列?

前端之家收集整理的这篇文章主要介绍了java – 为什么我的JTable不正确地排序整数列?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用DefaultTableModel的JTable,当用户点击列标题时,我允许进行排序.但是,当用户单击具有整数类型数据的列的标题时,它不能正确排序.看起来它是通过String排序而不是整型类型.

这里是我的代码实际添加到表中的部分:

DefaultTableModel aModel = (DefaultTableModel) mainView.logEntryTable.getModel();
                    ResultSetMetaData rsmd;             try {
            mainView.logEntriesTableModel.setRowCount(0);
            rsmd = rs.getMetaData();

            int colNo = rsmd.getColumnCount();
            while(rs.next()){
                Object[] objects = new Object[colNo];
                for(int i=0;i<colNo;i++){
                    objects[i]=rs.getObject(i+1);
                }
                aModel.addRow(objects);
                count++;
            }
            mainView.logEntryTable.setModel(aModel);
            mainView.logEntryTable.getColumnModel().getColumn(0).setMaxWidth(80);

所以我试图重写这个方法,最终结束了:

@Override
            public Class<?> getColumnClass(int columnIndex){
                if( columnIndex == 0){
                    // Return the column class for the integer column
                }else{
                    // Return the column class like we normally would have if we didn't override this method
                }

                return null;
            }
        };

我从来没有超过这个,我不太清楚我期望在这里做什么.

解决方法

尝试这个小例子.

更好的方法

如Kleopatra所建议的,定义与每个列相关的列类将足以使数据被正确排序.

import javax.swing.*;
import javax.swing.table.*;
import java.util.Comparator;

class TableSorting {
    public static void main(String[] args) {
        Object[][] data = {
            {new Integer(1),"Don't Let Go",new Integer(179)},{new Integer(2),"Photograph",new Integer(29)},{new Integer(3),"Hash Pipe",new Integer(186)},{new Integer(4),"Island In The Sun",new Integer(200)},{new Integer(5),"Crab",new Integer(154)},{new Integer(6),"Knock-Down Drag-Out",new Integer(128)},{new Integer(7),"Smile",new Integer(158)},{new Integer(8),"Simple Pages",new Integer(176)},{new Integer(9),"GlorIoUs Day",new Integer(160)},{new Integer(10),"O Girlfriend",new Integer(230)}
        };
        Object[] columns = {"Track #","Title","Length"};
        DefaultTableModel model = new DefaultTableModel(data,columns) {
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return Integer.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Integer.class;
                    default:
                        return String.class;
                }
            }
        };
        JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);
        table.setAutoCreateRowSorter(true);
        JOptionPane.showMessageDialog(null,scroll);
    }
}

原来,用比较器

import javax.swing.*;
import javax.swing.table.*;
import java.util.Comparator;

class TableSorting {
    public static void main(String[] args) {
        Object[][] data = {
            {new Integer(1),"Length"};
        JTable table = new JTable(data,columns);
        JScrollPane scroll = new JScrollPane(table);
        DefaultTableModel model = new DefaultTableModel(data,columns);
        TableRowSorter trs = new TableRowSorter(model);

        class IntComparator implements Comparator {
            public int compare(Object o1,Object o2) {
                Integer int1 = (Integer)o1;
                Integer int2 = (Integer)o2;
                return int1.compareTo(int2);
            }

            public boolean equals(Object o2) {
                return this.equals(o2);
            }
        }

        trs.setComparator(0,new IntComparator());

        table.setRowSorter(trs);

        scroll = new JScrollPane(table);
        table.setAutoCreateRowSorter(false);
        JOptionPane.showMessageDialog(null,scroll);
    }
}
原文链接:https://www.f2er.com/java/124694.html

猜你在找的Java相关文章