JTable在模型更新时不更新

时间:2013-05-12 21:50:45

标签: java swing jtable tablemodel

好的,我已经读过这篇文章,但我仍然很困惑。我有一个带有自定义表模型的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());
    }

以上代码来自我的主JFrame。不知道从哪里开始。

我现在迷路了。

1 个答案:

答案 0 :(得分:4)

该表初始化为:

jTable1.setModel(new AuthorModel(current_map.authors));

但是当单击该按钮时,您将修改变量author_model:

author_model.authors.add(new Author("New Author"));

表的初始化应该是

jTable1.setModel(author_model);

您还应该尊重Java命名约定,并为变量选择更好的名称。