Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   clear rows of JTable (http://www.velocityreviews.com/forums/t142471-clear-rows-of-jtable.html)

pat270881 04-05-2005 01:50 PM

clear rows of JTable
 
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

Arnaud Berger 04-05-2005 02:04 PM

Re: clear rows of JTable
 
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




"pat270881" <pat27@gmx.at> a écrit dans le message news:
afeee867.0504050550.74fc09e7@posting.google.com...
> 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




Nigel Wade 04-05-2005 04:32 PM

Re: clear rows of JTable
 
pat270881 wrote:

> 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.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555

natihern 05-12-2006 09:36 PM

Remove all the rows from a JTable
 
I used this sentence:

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

and it worked.:-D


Quote:

Originally Posted by 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


Abdul Jabbar WebBestow 11-23-2012 11:22 PM

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:


All times are GMT. The time now is 08:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.