68

How can i clear the content of the JTable using Java..

jmj
  • 237,923
  • 42
  • 401
  • 438
Chamal
  • 1,107
  • 6
  • 16
  • 23

7 Answers7

96

You must remove the data from the TableModel used for the table.

If using the DefaultTableModel, just set the row count to zero. This will delete the rows and fire the TableModelEvent to update the GUI.

JTable table;
…
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);

If you are using other TableModel, please check the documentation.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
user85421
  • 28,957
  • 10
  • 64
  • 87
  • Just adding this in here... I had to call `table.revalidate();` after setting the row count to zero in order to see the change immediately, otherwise I didn't see the change until I clicked the `JTable`. – Sakiboy Jun 12 '14 at 22:50
  • @Jim-Dingo works fine without `revalidate()` for me: [simple example](http://pastebin.com/iDgLeKPk) The `DefaultTableModel` informs the view when data was changed - check the code of `setNumRows(int)`, it calls `fireTableRowsDeleted` if the row count is decreased. – user85421 Aug 04 '14 at 09:33
  • perhaps @Sakiboy is using the older JDK so he need to revalidate() it everytime the model changed. – gumuruh Oct 30 '21 at 14:04
  • @gumuruh, yes this is an OLD answer – Sakiboy Nov 10 '21 at 16:37
22

Basically, it depends on the TableModel that you are using for your JTable. If you are using the DefaultTableModel then you can do it in two ways:

DefaultTableModel dm = (DefaultTableModel)table.getModel();
dm.getDataVector().removeAllElements();
dm.fireTableDataChanged(); // notifies the JTable that the model has changed

or

DefaultTableModel dm = (DefaultTableModel)table.getModel();
while(dm.getRowCount() > 0)
{
    dm.removeRow(0);
}

See the JavaDoc of DefaultTableModel for more details

Robe Elckers
  • 967
  • 1
  • 6
  • 19
  • 3
    Remember to fire an table event if you change the underlying data vector - else the JTable will have no clue that it has changed... – dacwe Jan 02 '11 at 12:23
  • 1
    -1, both suggestions are wrong. The first, for the reason commented above. The second will skip rows because the variable i keeps incrementing as you remove each row. If you want to create a loop then you just keep removing row 0, until there are no more rows. However, Carlos posted the simplest solution. – camickr Jan 02 '11 at 16:56
  • Camickr, you are right. I changed my answer to fix these issues. Still Carlos's solution is easier to implement. – Robe Elckers Jan 03 '11 at 17:26
7

I had to get clean table without columns. I have done folowing:

jMyTable.setModel(new DefaultTableModel());
Black_Zerg
  • 87
  • 2
  • 3
  • quick and effective solution but it also removes the header. It should be better if there's an option to not remove the header. Someone in another post suggested to remove all the rows one by one. That would keep the header – Jesus Flores Jun 04 '16 at 04:37
4

This is the fastest and easiest way that I have found;

while (tableModel.getRowCount()>0)
          {
             tableModel.removeRow(0);
          }

This clears the table lickety split and leaves it ready for new data.

Eduardo
  • 19,928
  • 23
  • 65
  • 73
1

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 is myTable, you can do following.

DefaultTableModel model = new DefaultTableModel();
myTable.setModel(model);
Malith
  • 381
  • 6
  • 18
1

If we use tMOdel.setRowCount(0); we can get Empty table.

DefaultTableModel tMOdel = (DefaultTableModel) jtableName.getModel();
tMOdel.setRowCount(0);
Madhuka Dilhan
  • 1,396
  • 1
  • 14
  • 21
  • 2
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. – abarisone Jul 06 '16 at 05:39
1
((DefaultTableModel)jTable3.getModel()).setNumRows(0); // delet all table row

Try This:

Shinwar ismail
  • 299
  • 2
  • 9