-1

我使用此代码获取我的身份证号码JTable

"AWT-EventQueue-0" java.lang.ClassCastException发生,

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == dellButton) {
        try {
            int rowToDelete = 0;
            int rowToModel = 0;
            if (table.getSelectedRow() > -1) {
                rowToDelete = table.getSelectedRow();
                rowToModel = table.convertRowIndexToModel(rowToDelete);
                tableModel.removeRow(rowToModel);
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
}

我的TableModel.removeRow()方法:

...
    public void removeRow(int rowNumber) throws SQLException {
    String removeQuery = "delete from mytable where id=?";
    PreparedStatement pStatement = con.prepareStatement(removeQuery);
    pStatement.setInt(1, rowNumber);
    int rowsAffected = pStatement.executeUpdate();
}
...
4

1 回答 1

4
  • int rowID = (int) table.getValueAt(selectedRowIndex, 0);是无用的代码行,

  • 代码行tableModel.removeRow(rowID, rowIndex);有错误的参数

  • 测试是否选择了任何行,否则选择的行返回 -1

  • 一切都可能只是

    @Override
    public void actionPerformed(ActionEvent e) {
        int rowToDelete = 0;
        int rowToModel = 0;
        if (table.getSelectedRow() > -1) {
            rowToDelete = table.getSelectedRow();
            rowToModel = table.convertRowIndexToModel(rowToDelete);
            model.removeRow(rowToModel);
        }
    }
    
  • 确保你SelectionMode愿意,否则你需要在选定行的数组中循环,

    table.getSelectionModel().setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
    
于 2013-07-30T21:54:29.980 回答