clear rows of JTable

P

pat270881

Hello,

I have a JFrame with a JTable where some lines with two columsn are
displayed. I want to write a function where all rows of the table are
cleared. My JTable has a DataTableModel model. So I thought I have to
remove the rows of the model to delete the rows from the JTable with
the following code:

for (int i = 0; i < model.getRowCount(), i++)
{
model.remove(i);
}

But when I use this code in that way, two rows of the JTable are not
deleted.

Can anybody help me with this problem?

thx

pat
 
A

Arnaud Berger

Hi,

Indeed, imagine that your model contains (in this order) : A, B and C
Your getRowCount equals 3

loop 1 : remove index 0 , A (B comes to index 0 , C comes to index 1)
loop 2 : remove index 1 , C
loop 3 : remove index 2 , but there is nothing at index 2 anymore !

Here you can see that B has never been removed.
The above demonstration is true , for instance, with a Vector based model.

If this is your problem, please consider using :

while (model.getRowCount()>0){
model.remove(0);
}

Regards,

Arnaud
 
N

Nigel Wade

pat270881 said:
Hello,

I have a JFrame with a JTable where some lines with two columsn are
displayed. I want to write a function where all rows of the table are
cleared. My JTable has a DataTableModel model. So I thought I have to
remove the rows of the model to delete the rows from the JTable with
the following code:

for (int i = 0; i < model.getRowCount(), i++)
{
model.remove(i);
}

But when I use this code in that way, two rows of the JTable are not
deleted.

Can anybody help me with this problem?

thx

pat

What is DataTableModel?

DefaultTableModel.setRowCount(0) would do what you want.
 
Joined
May 12, 2006
Messages
1
Reaction score
0
Remove all the rows from a JTable

I used this sentence:

DefaultTableModel dm = (DefaultTableModel)table.getModel();
dm.getDataVector().removeAllElements();

and it worked.:-D


pat270881 said:
Hello,

I have a JFrame with a JTable where some lines with two columsn are
displayed. I want to write a function where all rows of the table are
cleared. My JTable has a DataTableModel model. So I thought I have to
remove the rows of the model to delete the rows from the JTable with
the following code:

for (int i = 0; i < model.getRowCount(), i++)
{
model.remove(i);
}

But when I use this code in that way, two rows of the JTable are not
deleted.

Can anybody help me with this problem?

thx

pat
 
Joined
Nov 23, 2012
Messages
1
Reaction score
0
Simplest Way to Remove all Rows from JTable

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 got thousands of rows? looping????

:congrats:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top