• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting rows from a JTable  RSS feed

 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everytime I do a search, I want to start with
a fresh JTable.
The way I've been doing this is to call the
removeRow in a loop like this:
===========================================
for (int i = 1; i <= model.getRowCount(); i++)
{
model.removeRow(i);
}
model.fireTableRowsDeleted(firstRow, secondRow);
============================================
However, alhtough this code appears to do the trick, it doesn't completly clear all rows. Also,
in JBuilder, I get a bunch of exceptions.
What's the best way to clear out the entire
model (I'm doing it from a separate thread) every-
time the user does another search?
Thanks in advance.
-- Mike
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
I think, your instance of model is a DefaultTableModel and not one of your own. So you have two solutions, first you could write your own model, where you can implement a remmoveAll function. Second you leave it as it is and you delete your rows from the last element to the first element.

You have to do it that way, because the DefaultTableModel holds it's data in a Vector. And if you want to remove all your data from a vector in a for{;;} then you have to start at the end, because if you remove one element, then the size has changed and the last element has moved one step forward in the list.
So in your case, you try to remove elements from the model, which have changed the position.
Hope that's clear

Rene
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Much.
That's what I needed!!!
-- Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!