• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Removing rows from a default table model  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to remove rows from a default table model:

I tried

for ( int i=0; i<rowCount; i++ ) {
if ( (Boolean)dtm.getValueAt(i,16) )
dtm.removeRow(i);
}

This doesn't work as the index goes out of bounce. I think it is b/c when I remove a row the DTM has one less row. It felt wrong but then I tried

for ( int i=0; i<dtm.getRowCount(); i++ ) {
if ( (Boolean)dtm.getValueAt(i,16) )
dtm.removeRow(i);
}

This did not give an exception but the results were not consistent.

Does anyone have any suggestions?

Thanks
 
Marshal
Posts: 24473
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fabio Piergentili wrote:I think it is b/c when I remove a row the DTM has one less row.


Yes, that's right. This problem always occurs when you are trying to remove entries from a list by their index; it has nothing to do with table models in particular.

Suppose you have entries numbered 0, 1, 2, 3, 4 in your list. When you remove the entry at index 0 (which is 0), now you have 1, 2, 3, 4. If you then remove the entry at index 1 (which is 2), you now have 1, 3, 4. You should have the picture by now so I won't go on.

So if you want to remove the entries at index 0, 1, and 2, what you should actually do is to remove the entry at index 0 three times. Alternatively, remove the entries at index 2, 1, and 0 (in that order) and you won't run into this problem.


 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am removing based on if a value of a particular column is true or false ( random order ). I do not know ahead of time what I am deleting so you are telling me I have to figure what rows need to be deleted in one loop. Then in another loop, come up with a re-mapping scheme to delete the correct rows?

Thanks
 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to work ...

Boolean [] array = new Boolean [numberRows];
Vector rowsToRemove = new Vector();
if ( shipRadio.isSelected() ) {
for ( int i=0; i<numberRows; i++ ) {
if ( dtm.getValueAt(i,11) != null ) {
//array[i]=true;
rowsToRemove.add(i);
} else {
array[i]=false;
}
}

Iterator itr = rowsToRemove.iterator();
int i = 0;
while(itr.hasNext()) {
dtm.removeRow((Integer)itr.next() - i);
i++;
}
 
Rancher
Posts: 3040
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so you are telling me I have to figure what rows need to be deleted in one loop. Then in another loop, come up with a re-mapping scheme to delete the correct rows?



No, what Paul was saying is that you need to do the loop in reverse.

Instead of:


you use:



And use the "Code" tags so the code you post is readable.
 
Fabio Piergentili
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much nicer thanks for the help!

 
Would you like to try a free sample? Today we are featuring tiny ads:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!