Swing 现有的帖子不断被更新的帖子重新添加到jTable中

Swing 现有的帖子不断被更新的帖子重新添加到jTable中,swing,jtable,defaulttablemodel,Swing,Jtable,Defaulttablemodel,这是我的密码: public void submitReply(ActionEvent e) { String replyBy = userName; String reply = jTextArea_reply.getText(); if (reply.equals("")) { JOptionPane.showMessageDialog(null, "Comment cannot leave blank"); } else {

这是我的密码:

    public void submitReply(ActionEvent e) {
    String replyBy = userName;
    String reply = jTextArea_reply.getText();
    if (reply.equals("")) {
        JOptionPane.showMessageDialog(null, "Comment cannot leave blank");
    } else {
        eForumTopics comment = new eForumTopics(replyBy, reply);
        if (comment.createComment() == true) {
            JOptionPane
                    .showMessageDialog(null,
                            "Reply submitreted successfully. You will be redirect to main page.");
            SetUpJTableComment();

    public void SetUpJTableComment() {
    // Get jTable model
    DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
            .getModel();
    // Store column data into Array (3 columns)
    String[] data = new String[3];
    // Set Up Database Source
    db.setUp("IT Innovation Project");
    String sql = "Select reply_content,reply_by from forumReplies WHERE reply_topic = "
            + topicId + "";
    ResultSet resultSet = null;
    // Call readRequest to get the result
    resultSet = db.readRequest(sql);

    try {
        while (resultSet.next()) {
            data[0] = resultSet.getString("reply_content");
            data[1] = resultSet.getString("reply_by");
            // Add data to table model
            tableModel1.addRow(data);
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    // add tablemodel to jtable

}

问题是,每当用户发布新的回复时,现有的帖子都会被重新添加到一起。我试着只把评论框中更新的回复添加到jTable中,而不是继续用更新的回复重新添加现有的帖子。我应该用什么?一个for循环?提前感谢。

删除DefaultTableModel内容的正确方法是

model.setRowCount(0);
与评论中提到的邪恶方式(此处不再重复;-)相比,它违反了两条规则

  • 永远不要更改模型底部的底层数据结构
  • 切勿从模型外部的代码调用模型的任何fireXX

如果采取后一种方法似乎有帮助,那么这是一个警告信号:要么违反了前一种方法,要么模型实现不正确

同样的次优(被告知不需要再次设置模型)代码片段,如您上一个问题中所述-如何开始挖掘真正的错误?另外:学习java命名约定并坚持它们-现在我已经编辑了,那么我应该做什么来防止现有帖子继续重新添加?检查查询-它是否在每个调用中返回其他内容?您指的是哪个查询?是sql语句吗?如果是这样,sql语句将返回基于topicId的所有回复。然后我再次调用该方法,以便sql语句继续执行。但是我怎样才能过滤它呢?