2

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

Ignacio Nimo
  • 65
  • 3
  • 3
  • 9
  • 5
    `removelAll()` removes all components from a container and has nothing to do with JTable and it's data. The solution is to either get the table's TableModel via `getModel()` and clear the data from it, or give the JTable a new TableModel. But first and foremost, read the [JTable tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). It's all explained there, and your question suggests that you haven't done this basic step yet. You'll also want to check out the JTable API and that of DefaultTableModel which is the model used for your JTable. – Hovercraft Full Of Eels Feb 03 '12 at 21:22
  • Duplicate of http://stackoverflow.com/questions/3879610/clear-contents-of-a-jtable – james.garriss Oct 14 '13 at 12:52

2 Answers2

3

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.

Arpan
  • 596
  • 2
  • 10
  • 29
Raceimaztion
  • 9,494
  • 4
  • 26
  • 41
  • 1
    See also [*A Swing Architecture Overview*](http://java.sun.com/products/jfc/tsc/articles/architecture/). – trashgod Feb 04 '12 at 02:41
0

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);
        }
    }
}    
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
Saurabh Rd
  • 11
  • 1