在jTable中执行删除时出现ArrayIndexOutOfBound错误

时间:2013-01-18 14:29:48

标签: java sql swing defaulttablemodel

我尝试从jTable中选择某一行并执行删除,然后使用数据库中的最新数据更新jTable。这就是我设置jTable的方式:

    private JTable getJTableManageReplies() {
    jTableManageReplies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jTableManageReplies.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        int viewRow = jTableManageReplies.getSelectedRow();
                        // Get the first column data of the selectedrow
                        int replyID = Integer.parseInt(jTableManageReplies.getValueAt(
                                viewRow, 0).toString());

                        eForumRepliesAdmin reply = new eForumRepliesAdmin(replyID);
                        replyID = JOptionPane.showConfirmDialog(null, "Are you sure that you want to delete the selected reply? " , "Delete replies", JOptionPane.YES_NO_OPTION);
                        if(replyID == JOptionPane.YES_OPTION){
                        reply.deleteReply();
                        SetUpJTableManageReplies();}
                    }
                }
            });
    return jTableManageReplies;
}

     public void SetUpJTableManageReplies() {

    DefaultTableModel tableModel = (DefaultTableModel) jTableManageReplies
            .getModel();
    String[] data = new String[5];
    db.setUp("IT Innovation Project");
    String sql = "Select forumReplies.reply_ID,forumReplies.reply_topic,forumTopics.topic_title,forumReplies.reply_content,forumReplies.reply_by from forumReplies,forumTopics WHERE forumReplies.reply_topic = forumTopics.topic_id ";
    ResultSet resultSet = null;
    resultSet = db.readRequest(sql);
    tableModel.getDataVector().removeAllElements();

    try {
        while (resultSet.next()) {
            data[0] = resultSet.getString("reply_ID");
            data[1] = resultSet.getString("reply_topic");
            data[2] = resultSet.getString("topic_title");
            data[3] = resultSet.getString("reply_content");
            data[4] = resultSet.getString("reply_by");
            // Add data to table model
            tableModel.addRow(data);
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

这是我从数据库中执行删除的代码:

    public boolean deleteReply() {
    boolean success = false;
    DBController db = new DBController();
    db.setUp("IT Innovation Project");
    String sql = "DELETE FROM forumReplies where reply_ID = " + replyID
            + "";
    if (db.updateRequest(sql) == 1)
        success = true;
    db.terminate();
    return success;
}

但是,在jDialog框中添加SetUpJTableManageReplies方法后,会出现一条错误消息,即ArrayIndexOutOfBound。我尝试做用户选择某一行时,会弹出一个要求确认删除。然后在他们点击是之后,jTable数据将被刷新。有人可以给我一些指导吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

你的问题在这里:

tableModel.getDataVector().removeAllElements();

更好:

tableModel.setRowCount(0);

好多了:编写自己的表模型并实现TableModel接口中定义的所有方法 - 这样您就可以学习如何处理JTable组件

答案 1 :(得分:0)

使用TableModel管理表数据。 DefaultTableModel将非常有用,您应首先创建tableModel,然后创建JTable并将表模型设置为先前创建的表模型。

您应该使用model对表格单元格执行插入/删除/更新,这将自动更新JTable。使用DefaultTableModel来管理您的数据。