好吧,我已经阅读了这篇文章,但我仍然很困惑。我有一个带有自定义表模型的 JTable,它将数据存储在 ArrayList 中。它显示得很好。但是,当我想添加一行时,我将一个对象添加到 ArrayList,然后调用 fireTableRowsInserted(...)。但是,该表不会刷新。
public Main() {
initComponents();
current_map = new Map();
current_map.authors.add(new Author("New Author"));
author_model = new AuthorModel(current_map.authors);
jTable1.setModel(new AuthorModel(current_map.authors)); <---This is the mistake
}
...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
author_model.authors.add(new Author("New Author"));
author_model.fireTableRowsInserted(author_model.authors.size(), author_model.authors.size());
}
上面的代码来 self 的主 JFrame。不知道从这里去哪里。
我现在迷路了。
最佳答案
表初始化为:
jTable1.setModel(new AuthorModel(current_map.authors));
但是当单击按钮时,您会修改变量author_model:
author_model.authors.add(new Author("New Author"));
表的初始化应该是
jTable1.setModel(author_model);
您还应该尊重 Java 命名约定,并为变量选择更好的名称。
关于java - 模型更新时 JTable 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512401/