18

I have a jTable and it's got a table model defined like this:

javax.swing.table.TableModel dataModel = 
     new javax.swing.table.DefaultTableModel(data, columns);
tblCompounds.setModel(dataModel);

Does anyone know how I can clear its contents ? Just so it returns to an empty table ?

Arpan
  • 596
  • 2
  • 10
  • 29
tom
  • 4,911
  • 11
  • 37
  • 39

7 Answers7

32

Easiest way:

//private TableModel dataModel;
private DefaultTableModel dataModel;


void setModel() {
  Vector data = makeData();
  Vector columns = makeColumns();
  dataModel = new DefaultTableModel(data, columns);
  table.setModel(dataModel);
}

void reset() {
  dataModel.setRowCount(0);
}

i.e. your reset method tell the model to have 0 rows of data The model will fire the appropriate data change events to the table which will rebuild itself.

camickr
  • 321,443
  • 19
  • 166
  • 288
locka
  • 5,809
  • 3
  • 33
  • 38
  • 1
    +1, (with a slight change), the TableModel interface does not have a setRowCount() method. That method is found in the DefaultTableModel class. I edited your sample code to use the DefaultTableModel, not the TableModel. – camickr Oct 07 '10 at 16:10
  • -100 This is not the correct answer if I understood the question. In this question the most times we mean and we want to empty cells in the table and ΝΟΤ to have a TABLE WITHOUT CELLS! Follow the answer of @Gebreigziabher Abadi below as the most correct, but changing the `dm.setValueAt("", i, j);` with `dm.setValueAt(null, i, j);` – Universe Nov 05 '14 at 14:53
12

If you mean to remove the content but its cells remain intact, then:

public static void clearTable(final JTable table) {
   for (int i = 0; i < table.getRowCount(); i++) {
      for(int j = 0; j < table.getColumnCount(); j++) {
          table.setValueAt("", i, j);
      }
   }
}

OK, if you mean to remove all the cells but maintain its headers:

public static void deleteAllRows(final DefaultTableModel model) {
    for( int i = model.getRowCount() - 1; i >= 0; i-- ) {
        model.removeRow(i);
    }
}
eee
  • 1,043
  • 7
  • 5
  • Sorry, i want to return the table to a state where its just got the column headers but no data in it – tom Oct 07 '10 at 08:00
  • (virtual) -1 for the second part: as long as you know that the type of the model is DefaultTableModel, the correct method to call is setRowCount(0). – kleopatra Sep 11 '12 at 12:32
4
    //To clear the Contents of Java JTable

    DefaultTableModel dm = (DefaultTableModel) JTable1.getModel();

    for (int i = 0; i < dm.getRowCount(); i++) {
        for (int j = 0; j < dm.getColumnCount(); j++) {
            dm.setValueAt("", i, j);
        }
    }
rom_j
  • 9,010
  • 2
  • 14
  • 16
  • 1
    judging by the fact that the OP accepted an answer that _removed all rows_ emptying the _cell content_ doesn't seem what s/he wanted to achieve :-) And if that were the goal, setting the content to an empty string is not emptying it and might blow up in your face (if the model expects another type): so better null the content. – kleopatra Dec 25 '13 at 13:16
2

You have a couple of options:

  1. Create a new DefaultTableModel(), but remember to re-attach any listeners.
  2. Iterate over the model.removeRow(index) to remove.
  3. Define your own model that wraps a List/Set and expose the clear method.
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
0

I think you meant that you want to clear all the cells in the jTable and make it just like a new blank jTable. For an example, if your table contains 40 raws, you can do following.

DefaultTableModel model = (DefaultTableModel)this.jTable.getModel();
model.setRowCount(0);
model.setRowCount(40);
Malith
  • 381
  • 6
  • 18
0

One of the trivial methods is to use the following option.

dataModel.setRowCount(0);

dataModel is the model which you would like clear the content on

However, it is not optiomal solution.

Farruh Habibullaev
  • 2,342
  • 1
  • 26
  • 33
-1

Another easy answer:

defaultTableModel.getDataVector().removeAllElements();
james.garriss
  • 12,959
  • 7
  • 83
  • 96
  • no, that's completely wrong: never-ever change the data under the feet of the model, instead use model api – kleopatra Sep 11 '12 at 12:30