-1

Here are my codes :

    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

}

The problem is whenever users post a new reply, the existing posts will be re-added together. I try to do like only the newer reply from the comment box will be added into the jTable instead of keep on re-add the existing posts with newer reply. What am I supposed to use? A for loop? Thanks in advance.

It newbie
  • 9
  • 4
  • same suboptimal (you were told that there's no need for setting the model again) code snippet as in your last question - how about start digging into what's really wrong? – kleopatra Jan 15 '13 at 11:54
  • Plus: learn java naming conventions and stick to them - **now** – kleopatra Jan 15 '13 at 11:57
  • I edited already so what am I supposed to do to prevent the existing posts to keep on re-adding? – It newbie Jan 15 '13 at 11:58
  • check the query - does it return something else in each call? – kleopatra Jan 15 '13 at 12:01
  • Which query you mean? Is it the sql statement? If so, the sql statement is returning all the replies based on topicId. Then I called the method again so the sql statement will keep on executed. But how can I filter it? – It newbie Jan 15 '13 at 12:04
  • so you are adding the same data if the topicId didn't change? If so why are you surprised they are contained more than once? Hint: you can clear the model content :-) – kleopatra Jan 15 '13 at 12:07
  • Wait wait, I am a little bit of confusing now. If I clear the model content, nothing appear in my table. – It newbie Jan 15 '13 at 12:10
  • Sorry I misunderstand your clear the model content. After I add in : tableModel1.getDataVector().removeAllElements(); tableModel1.fireTableDataChanged(); and it works now. Thanks alot – It newbie Jan 15 '13 at 12:24

1 Answers1

0

The correct way to delete the content of DefaultTableModel is

model.setRowCount(0);

vs. the evil way mentioned in the comment (won't repeat it here ;-) which violates two rules

  • never change the underlying datastructure of a model under its feet
  • never call any of the model's fireXX from code outside the model

If doing the latter seems to help, it's a waring signal: you either violated the former or your model implementation is incorrect

kleopatra
  • 51,061
  • 28
  • 99
  • 211