简体   繁体   中英

Sorting numbers in JTable

Please, could anyone see why this code still refuse to use my comparator ? This is all code relative to userTable object + I have mouse and key listener there, but I think it is not affecting sorting.

private TableRowSorter trs;

public constructor() {
initComponents();
...
trs = new TableRowSorter<>(userTable.getModel());

    class IntComparator implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            System.out.println("comparing");
            Integer int1 = (Integer) o1;
            Integer int2 = (Integer) o2;
            return int1.compareTo(int2);
        }

    }

    trs.setComparator(0, new IntComparator());
    userTable.setRowSorter(trs);
...
}

private void initComponents() {
...
userTable = new javax.swing.JTable(){
            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                return renderer;
            }
        };
...
userTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "count", "null"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

}

It keeps doing RADIX sort and also message "comparing" s never shown. Thank you.

http://download.java.net/jdk8/docs/api/javax/swing/DefaultRowSorter.html#setComparator(int , java.util.Comparator)

  1. You probably need to call "sort"
  2. You probably need to specify "1" as starting column (not sure, just a hunch)

Problem was, that table was filled from DB using JDBS, where I did not set proper data type. Table column itself was set as integer, or float, but data readed from DB was considered for string.

switch (rs.getMetaData().getColumnType(i + 1)) {
                        case 93: //DATE
                            rowData[i] = rs.getDate(i + 1).toString() + "> " + rs.getTime(i + 1);
                            break;
                        default:
                            rowData[i] = rs.getString(i + 1);
                            break;
                    }

New code looks like this.

switch (rs.getMetaData().getColumnType(i + 1)) {
                        case 93:
                            rowData[i] = rs.getDate(i + 1).toString() + "> " + rs.getTime(i + 1);
                            break;
                        case 4:
                            rowData[i] = rs.getInt(i + 1);
                            break;
                        case -5:
                            rowData[i] = rs.getLong(i + 1);
                            break;
                        case 7:
                            rowData[i] = rs.getFloat(i + 1);
                            break;
                        default:
                            rowData[i] = rs.getString(i + 1);
                            break;
                    }

Jtable has default row sorterer builded by checkbox in netbeans :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM