Problems with JTable sorting of integer values

前端 未结 6 1056
故里飘歌
故里飘歌 2020-12-21 10:44

Currently I have a JTable that uses RowSorter, but when I click the header that I want it to sort in, it displays the rows in a weird order

6条回答
  •  难免孤独
    2020-12-21 11:26

    You can set the column type for a JTable by setting its model explicitly like in the following example

    setModel(new DefaultTableModel(new Object[0][], new String[] {
                    "SELECT", "WHERE", "FIELD", "TYPE" }) {
                Class[] types = { Boolean.class, Boolean.class, String.class,
                        String.class };
                boolean[] canEdit = { true, false, false, false };
    
                @Override
                public Class getColumnClass(int columnIndex) {
                    return this.types[columnIndex];
                }
    
                public boolean isCellEditable(int columnIndex) {
                    return this.canEdit[columnIndex];
                }
            });
    

    Give your column classes like this (here column one and two are Boolean and the rest String.

     Class[] types = { Boolean.class, Boolean.class, String.class,String.class };
    

提交回复
热议问题