2

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.

Chris Chambers
  • 1,367
  • 21
  • 39

1 Answers1

4

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Oh, duh, how did I miss that one. I know the names are not good, they are temporary. – Chris Chambers May 12 '13 at 21:56
  • 3
    author_model should be authorModel. Same for current_map. Variables are camelCased in Java. – JB Nizet May 12 '13 at 21:56
  • In addition, with regards to "temporary names": you should *always* put thought into even the simplest of projects. Underestimation has time and time again proven to be a problem (e.g., creating the internet? who would EVER need more than 4 billion IP addresses?). If you ever end up expanding this project - or even just copying some of the code to use in another project - issues like these will befuddle you and others. And if this somehow gets into a public API... \*shudder\* – wchargin May 12 '13 at 22:56
  • I would echo this, also that effort put into naming things is always worth it. Get into the habit of telling your readers the truth about everything you name: variables, classes, methods, packages. The chances that those names live on after a "temporary" period is too great. Besides, the greatest need for them is at the beginning, while you're trying to debug things like this... – arcy May 13 '13 at 00:54