为什么我的JTable排序整数不正确?

时间:2018-04-19 19:26:16

标签: java swing jtable

我的JTable将整数排序为1,10,100,101。看起来它仍然将其排序为字符串。我已经查看了类似的问题并尝试了必要的调整。我错过了什么。我使用netbeans GUI创建者创建了这个,如果它改变了什么。我也使用方法

listSubTable.setModel(DbUtils.resultSetToTableModel(rs));

在代码的另一部分。这会将结果集发送到表中。这是否优先于下面的代码。我将switch语句更改为所有整数,以防我遇到混淆列。它仍然将它们列为字符串。

    listSubTable = new javax.swing.JTable(){
    public boolean isCellEditable(int rowIndex, int colIndex) {

        return false;   //Disallow the editing of any cell
    }

};
listSubTable.setAutoCreateRowSorter(true); 
listSubTable.addMouseListener(new MouseAdapter() {
  public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1 && isSubdivision()) {
      JTable target = (JTable)e.getSource();
      int row = target.getSelectedRow();
      int column = target.getSelectedColumn();
     if (listSubTable.getSelectedRow() != -1) {

                row = listSubTable.getSelectedRow();
                archiveNameField.setText("PTC_" + listSubTable.getModel().getValueAt(row, column).toString() + "-V");

            }
    }
if (e.getClickCount() == 1 && !isSubdivision()) {
      JTable target = (JTable)e.getSource();
      int row = target.getSelectedRow();
      int column = target.getSelectedColumn();
     if (listSubTable.getSelectedRow() != -1) {

                row = listSubTable.getSelectedRow();
                archiveNameField.setText("");
            }
    }
  }
});
listSubTable.setModel(new javax.swing.table.DefaultTableModel(
    new Object [] [] {},
    new Integer [] {

    })
    {@Override
        public Class getColumnClass(int column) {
            switch (column) {
                case 0:
                return Integer.class;
                case 1:
                return Integer.class;
                case 2:
                return Integer.class;
                default:
                return Integer.class;
            }
        }}

        );

listSubTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);



listSubTable.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        listSubTableMouseClicked(evt);
    }
});

jScrollPane3.setViewportView(listSubTable);

1 个答案:

答案 0 :(得分:0)

看起来问题就在这一行:

listSubTable.setModel(DbUtils.resultSetToTableModel(rs));

一定是把所有东西都当作一个字符串。我将其替换为以下内容。

            ResultSet rs = stmt.executeQuery(sql);
            while (listSubTable.getRowCount() > 0) {
                ((DefaultTableModel) listSubTable.getModel()).removeRow(0);
            }
            int obColumns = rs.getMetaData().getColumnCount();
            System.out.println("obColumns: " + obColumns);
            while (rs.next()) {
                Object[] obRow = new Object[obColumns];
                obRow[0] = rs.getString(1);
                obRow[1] = rs.getInt(2);
                System.out.println(obRow[0]);
                System.out.println(obRow[1]);

           ((DefaultTableModel)listSubTable.getModel()).insertRow(rs.getRow() - 1, obRow);
            }