0

我尝试在删除选定行时刷新 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();
                        JOptionPane.showMessageDialog(null, "Reply has been deleted successfully.");
                        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);

    jTableManageReplies.repaint();
    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");
            tableModel.addRow(data);
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

这是我的 sql 语句:

    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;
}

我调用了 repaint() 来用数据库中的最新数据更新表数据,它可以工作。我的意思是删除某些行后的数据。但是,现有的帖子将继续重新添加。然后我添加 removeAllElement 方法来删​​除所有现有的帖子,因为我的 sql 语句是 select * from table。然后,有一条错误消息,即 ArrayIndexOutOfBoundsException。有什么指南可以解决这个问题吗?提前致谢。

4

1 回答 1

1

我调用了 repaint() 来用数据库中的最新数据更新表数据,它可以工作。

repaint数据更改时无需调用方法。数据更改由Table Model(在这种情况下为DefaultTableModelfireXXXMethods )处理。并且在数据更改时需要调用,但您正在使用DefaultTableModel甚至不需要的那些。(因为默认情况下,只要有变化,它就会调用这些方法。)

我认为问题出在valuesChanged(..)方法上。您正在获取第 0 行的值,但不检查表是否有行。所以保持一个约束。

int viewRow = jTableManageReplies.getSelectedRow();
// Get the first column data of the selectedrow
if(jTableManageReplies.getRowCount() > 0)
    int replyID = Integer.parseInt(jTableManageReplies.getValueAt(viewRow, 0).toString());
于 2013-01-19T05:11:07.557 回答