Chris Chambers
Chris Chambers

Reputation: 1365

JTable not updating when model updates

Ok, I have read up on this, but I'm still very confused. I have a JTable with a custom Table Model that stores data in an ArrayList. It displays just fine. However, when I want to add a row, I add an object to the ArrayList, then call fireTableRowsInserted(...). However, the table does not refresh.

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());
    }

The above code is from my main JFrame. Not sure where to go from here.

I'm lost right now.

Upvotes: 2

Views: 1053

Answers (1)

JB Nizet
JB Nizet

Reputation: 692281

The table is initialized with:

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

But when the button is clicked, you modify the variable author_model:

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

The initialization of the table should be

jTable1.setModel(author_model);

You should also respect the Java naming conventions, and choose better names for your variables.

Upvotes: 4

Related Questions