-1

我尝试从 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。当用户选择某行时,我会尝试这样做,会弹出要求确认删除。然后在他们单击yes 之后,jTable 数据将被刷新。有人可以给我一些指南吗?提前致谢。

4

2 回答 2

1

你的问题在这里:

tableModel.getDataVector().removeAllElements();

更好的:

tableModel.setRowCount(0);

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

于 2013-01-18T15:39:28.007 回答
0

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

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

于 2013-01-18T15:40:40.893 回答