以下是我的代码:
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循环?提前谢谢。
答案 0 :(得分:0)
删除DefaultTableModel内容的正确方法是
model.setRowCount(0);
VS。评论中提到的邪恶方式(这里不再重复;-)违反两条规则
如果做后者似乎有帮助,那就是一个警告信号:你要么违反了前者,要么你的模型实施不正确