JTable 将数字作为字符串进行排序

JTable keeps sorting numbers as Strings

我想对 table 进行正确排序,比较正确的 classes,而不是字符串。 我正在使用 TableRowSorter:

TableRowSorter sorter = new TableRowSorter<CustomModelTable>(modelData);
tablaEJ.setRowSorter(sorter);

我重写了 CustomModelTable 中的 getColumnClass 方法,因此我根据列的名称获得了正确的 class(这是因为类型是通过编程方式定义的):

@Override
    public Class getColumnClass(int columnIndex)
    {
        try {
            if (columnIndex < this.getColumnCount()) {
                String name = getColumnName(columnIndex); //This just gets the header of the column
                name = name.toLowerCase();
                switch (name) {
                    case "int":
                    case "int[]":
                    case "núm.":
                    case "int[][]": return Integer.class; //Or Class.forName("java.lang.Integer");
                    case "byte":
                    case "byte[]":
                    case "byte[][]": return Class.forName("java.lang.Byte");
                    case "long":
                    case "long[]":
                    case "long[][]": return Class.forName("java.lang.Long");
                    case "float":
                    case "float[]":
                    case "float[][]": return Class.forName("java.lang.Float");
                    case "double":
                    case "double[]":
                    case "double[][]": return Class.forName("java.lang.Double");
                    case "boolean":
                    case "boolean[]":
                    case "boolean[][]": return Class.forName("java.lang.Boolean");
                    case "char":
                    case "char[]":
                    case "char[][]": return Class.forName("java.lang.Character");
                    case "string":
                    case "string[]":
                    case "string[][]": return Class.forName("java.lang.String");
                    default: return Class.forName("java.lang.String");
                }
            }
        } catch (Exception e){

        }
        return null;
    }

截图如下: Table wrongly sorted

最奇怪的是我打印了类型:

for (int i = 0; i < tablaEJ.getModel().getColumnCount(); i++) {
     System.out.println(tablaEJ.getModel().getColumnClass(i));
}

它returns 预期输出(上面table 的打印):

class java.lang.Integer
class java.lang.Integer
class java.lang.Integer
class java.lang.Integer

老实说,我不知道我做错了什么。

我认为你不应该这样做。 定义列时,您应该在设置任何内容之前实例化列的类型。

“安全”的方法是声明哪些列索引是数字:

public static class CustomTableModel extends DefaultTableModel {
        private final List<Integer> columnIndicesWithNumbers = Arrays.asList(0);

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndicesWithNumbers.contains(columnIndex))
                return Double.class;
            return super.getColumnClass(columnIndex);
        }
    }

这里有一个完整的例子来测试你自己:

public class SorterTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            DefaultTableModel dm = new CustomTableModel();
            dm.addColumn("Integers");
            dm.addColumn("Strings");
            //@formatter:off 
            Object[][] data = { 
                    { 1, "Something" }, { 102, "Something Else" }, 
                    { 55, "Something" }, { 66, "Something" },
                    { 1000, "Something" }, { 1524, "Something" }, 
                    { 5801, "Something" }, { -55, "Something" },};
            //@formatter:on
            for (Object[] row : data) {
                dm.addRow(row);
            }
            JTable table = new JTable(dm);
            table.setAutoCreateRowSorter(true);

            JOptionPane.showMessageDialog(null, new JScrollPane(table));
        });
    }

    public static class CustomTableModel extends DefaultTableModel {
        private final List<Integer> columnIndicesWithNumbers = Arrays.asList(0);

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndicesWithNumbers.contains(columnIndex))
                return Double.class;
            return super.getColumnClass(columnIndex);
        }
    }
}

预览:

在你在评论区澄清之后:

您可以 @Override getValueAt 方法以 return 整数而不是字符串。

完整示例:

public class SorterTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            DefaultTableModel dm = new CustomTableModel();
            dm.addColumn("Integers");
            dm.addColumn("Strings");
            //@formatter:off 
            Object[][] data = { 
                    { "1", "Something" }, { "102", "Something Else" }, 
                    { "55", "Something" }, { "66", "Something" },
                    { "1000", "Something" }, { "1524", "Something" }, 
                    { "5801", "Something" }, { "-55", "Something" },};
            //@formatter:on
            for (Object[] row : data) {
                dm.addRow(row);
            }
            JTable table = new JTable(dm);
            table.setAutoCreateRowSorter(true);

            JOptionPane.showMessageDialog(null, new JScrollPane(table));
        });
    }

    public static class CustomTableModel extends DefaultTableModel {
        private final List<Integer> columnIndicesWithNumbers = Arrays.asList(0);

        @Override
        public Object getValueAt(int row, int column) {
            Object value = super.getValueAt(row, column);
            if (columnIndicesWithNumbers.contains(column)) {
                return Double.parseDouble(String.valueOf(value));
            }
            return value;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndicesWithNumbers.contains(columnIndex))
                return Double.class;
            return super.getColumnClass(columnIndex);
        }
    }
}