Ignacio Nimo
Ignacio Nimo

Reputation: 65

How to clear data from a JTable?

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

Upvotes: 2

Views: 15929

Answers (2)

Saurabh Rd
Saurabh Rd

Reputation: 11

The best solution for your question is

private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {
    DefaultTableModel model = (DefaultTableModel)UR_TABLEVARIABLENAME.getModel();

    while (model.getRowCount() > 0){
        for (int i = 0; i < model.getRowCount(); ++i){
            model.removeRow(i);
        }
    }
}    

Upvotes: 0

Raceimaztion
Raceimaztion

Reputation: 9634

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.

Upvotes: 3

Related Questions