How to clear data from a JTable?

Ignacio Nimo picture Ignacio Nimo · Feb 3, 2012 · Viewed 15k times · Source

I am programming at Netbeans, and I have a jTable in a frame.

In which I load data that occupies a lot of rows, but then i load another table that has a lot less rows.

And when i am running it, and loading the 2nd table, the extra rows that the first table had are still appearing there. And i wish to just see the 2nd table.

I already tried for jTable.removeAll();

Answer

Raceimaztion picture Raceimaztion · Feb 3, 2012

JTable uses the Model/View/Controller methodology, which means that the JTable class is both the View and Controller, so you need to either replace the TableModel by using JTable.setModel(newModel) or clear the TableModel by using JTable.getModel() and clearing the model that way.

See the tutorial on using tables in the JTable Tutorials.