33

I need to remove all the rows in my JTable.

I have tried both of the following:

/**
 * Removes all the rows in the table
 */
public void clearTable()
{
    DefaultTableModel dm = (DefaultTableModel) getModel();
    dm.getDataVector().removeAllElements();
    revalidate();
}

and

((DefaultTableModel)table.getModel()).setNumRows(0);

Neither of which would remove all the rows. Any ideas?

6
  • @MByD I just added revalidate(), but still seeing the same thing. I am using a CustomeTableCellRenderer, but wouldnt think that would do too much.
    – user489041
    Commented Jun 3, 2011 at 20:36
  • I tried this as well. No luck. I think there is something else going on.
    – user489041
    Commented Jun 3, 2011 at 20:43
  • 1
    @MByD, no you shouldn't revalidate() or repaint(). The TableModel is responsible for notifying the JTable that something has changed so the table can repaint itself automatically.
    – camickr
    Commented Jun 4, 2011 at 1:42
  • @camickr - that for the information :)
    – MByD
    Commented Jun 4, 2011 at 1:44
  • voting to close as not-a-real-question - it's not answerable as posted, and evokes one incorrect pseudo-answer after another ...
    – kleopatra
    Commented Nov 24, 2012 at 11:20

15 Answers 15

93

We can use DefaultTableModel.setRowCount(int) for this purpose, refering to Java's Documentation:

public void setRowCount(int rowCount)

Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded.

This means, we can clear a table like this:

DefaultTableModel dtm = (DefaultTableModel) jtMyTable.getModel();
dtm.setRowCount(0);

Now, on "how does java discard those rows?", I believe it just calls some C-like free(void*) ultimately somewhen, or maybe it just removes all references to that memory zone and leaves it for GC to care about, the documentation isn't quite clear regarding how this function works internally.

4
  • 4
    Great and simple code. Although the flagged answer to this question is also correct, it has problem when it comes on deleting thousands of rows. If you're handling thousands of row, this code is a snap. Commented Jan 18, 2015 at 15:29
  • 2
    This one clear all table rows. Easy and usable. Thanks Commented May 7, 2015 at 14:06
  • 1
    This is the best answer.
    – user1300214
    Commented Jun 17, 2017 at 17:14
  • Best answer for mee too, and with this is not necessary to iter in all table rows. Quite a strange method I would say.
    – TheGabiRod
    Commented Nov 9, 2018 at 18:10
41

The following code worked for me:

DefaultTableModel dm = (DefaultTableModel) getModel();
int rowCount = dm.getRowCount();
//Remove rows one by one from the end of the table
for (int i = rowCount - 1; i >= 0; i--) {
    dm.removeRow(i);
}
7
  • 7
    -1, Actually, this code doesn't work. First you remove row 0, then all the rows shift down 1. Then you delete row 1, which means row 0 is still there. James_Bond's answer shows how this is done by deleting from the end not the start.
    – camickr
    Commented Jun 4, 2011 at 1:42
  • 2
    Yes, you're right, the array elements have to be removed from back to front.
    – Mihai
    Commented Jun 4, 2011 at 6:22
  • 1
    no need for looping - DefaultTableModel has api to clear all rows with a single method call ...
    – kleopatra
    Commented Sep 25, 2012 at 16:48
  • 1
    Wondering how this code can work with the i++. Should probably be i--
    – Robin
    Commented Sep 26, 2012 at 5:51
  • 2
    It doesn't work, it will only delete half of the table, and throw an exception after that. You could replace i inside the loop with 0.
    – AdelaN
    Commented Aug 30, 2014 at 18:00
22

Something like this should work

DefaultTableModel model = (DefaultTableModel)this.getModel(); 
int rows = model.getRowCount(); 
for(int i = rows - 1; i >=0; i--)
{
   model.removeRow(i); 
}
3
  • 1
    Assuming a typical array-like backing, this is far better than removing the elements front to back. Commented Jun 3, 2011 at 21:05
  • btw: it should be DefaultTableModel, TableModel has no method removeRow
    – AvrDragon
    Commented Sep 25, 2012 at 7:34
  • 1
    no need for looping, simply use the api that is meant for clearing the data (not too obvious, but then you probably read @camickr 's answer - which mentions the deprecated method)
    – kleopatra
    Commented Sep 25, 2012 at 16:47
14

Read the API for DefaultTableModel - setRowCount method supports deleting/discarding all rows in one go...

((DefaultTableModel)myTable.getModel()).setRowCount(0);

1
  • 1
    correct, but nothing new: there is already an earlier answer pointing to that exact method :-) Plus more than one pointing to the older api ..
    – kleopatra
    Commented Jan 21, 2014 at 15:50
9

Well, setNumRows(0) should work, although if you actually read the API it tells you that this method is obsolete and tell you which method to use instead.

If the code doesn't work, then you are doing something else wrong and we can't tell from the posted code what that might be.

Post your SSCCE that demonstrates the problem.

9

The simplest way to remove all rows from JTable, just use this method instead...

tablemodel.getDataVector().removeAllElements();
tablemodel.fireTableDataChanged();

tablemodel is the model which you created for your table to add new rows. This is the shortest and fastest way of deleting all rows because what if you have thousands of rows? Looping?

3
  • might appear simple - but is wrong: a) the general rule is to never-ever call any fireXX from outside of the model, notifying its listeners is the exclusive responsibility of the model itself. b) DefaultTableModel has api to clear remove all rows (though its name is unfortunate), there's absolutely no need to interact with the underlying data structure
    – kleopatra
    Commented Nov 24, 2012 at 11:10
  • @kleopatra could you please explain the rule you are talking about? i dont understand why i can't use any fireXX method when they are public and accessible by default implementation!
    – Hazhir
    Commented Aug 27, 2013 at 16:21
  • it's a design accident that they are public - plain OO sanity requires that a) the class does its job completely and b) outsiders never-ever take over the job someone else is responsible for
    – kleopatra
    Commented Aug 28, 2013 at 6:45
6
try{

    DefaultTableModel dtm = (DefaultTableModel) jTable2.getModel();

    dtm.setNumRows(0); 

}catch(Exception e){
}
0
1

Or if you have lots of rows but very few columns...

DefaultTableModel dtm = new DefaultTableModel();
for(int i=0;i<NUM_COLS;i++) dtm.addColumn(COLUMN_NAME[i]);
myTable.setModel(dtm);

...replaces the old DTM with a fresh one.

1
  • 1
    while possible, would not recommended (especially, as it's not needed, the DTM has api to remove all its rows) - the implication is that listeners to the old model must be re-wired to the new model. Not rocket science, though, just unnecessary complexity ;-)
    – kleopatra
    Commented Nov 24, 2012 at 11:16
0

This worked for me, just use

tableModel.setRowCount(0); or tableModel.setNumRows(0);

Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded. Params: rowCount – number of rows in the model

By setting the number of rows to zero you're are telling the table moble to have no rows, which makes the table empty.

-1

Try this code. This will remove all the rows from JTable.

DefaultTableModel model=new DefaulTableModel(rows,cols);
JTable table=new JTable(model);
for(int i=0;i<model.getRowCount();i=i+0)
{
 model.removeRow(0);
 revalidate();
 }
1
  • -1 sigh - yet another variant of an incorrect answer ... NO there is no need at all to loop and NO there is even less a need to revalidate anything in that loop
    – kleopatra
    Commented Apr 20, 2013 at 12:37
-1

I had multiple tables, so I created a method to clear "any" table:

private void deleteAllTableRows(JTable table) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    while( model.getRowCount() > 0 ){
        model.removeRow(0);
    }
}
-1
DefaultTableModel tm = (DefaultTableModel) tbl.getModel();
while(tbl.getRowCount() > 0)
{
    ((DefaultTableModel) tbl.getModel()).removeRow(0);
}
1
  • While this code may help answer the question, code only answers are not high quality. A better answer would explain what the code does, tell where to insert it, explain why this approach was taken, and link to relevant documentation. Commented Jun 23, 2014 at 2:13
-1
MyModel myTableModel = (MyModel) myTable.getModel();
for (int i = myTableModel.getRowCount()-1; i >= 0; i--) myTableModel.deleteRow(i);
-1

You can not simple use for loops here because in every loop you remove a row which decreases the row count. Here's the right one:

int count = tmodel.getRowCount();
    while(count > 0) {
        tmodel.removeRow(count-1);
        count = tmodel.getRowCount();
    }
-2
    DefaultTableModel tb = (DefaultTableModel) jTable1.getModel();
    if (jTable1.getSelectedRow() == 0) {
    } else {
        jTable1.selectAll();
        int t = jTable1.getSelectedRow();
        while (t >= 0) {
            tb.removeRow(t);
        }
    }
1
  • 2
    Please read How do I write a good answer?. While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. - From Review Commented Jul 20, 2022 at 5:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.