deleting rows from a JTable

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

deleting rows from a JTable

 
0
  #1
Jun 20th, 2005
I know that you can call this method to delete a row from a JTable:
  1.  
  2. DefaultTableModel.deleteRow(index);

I need to delete all of the rows at some point, and this is not working:
  1. for (int i=0; i<dtm.getRowCount(); i++)
  2. {
  3. dtm.removeRow(i);
  4. mortgageTable.revalidate();
  5. }

I tried revalidating, but that didn't work. This only deletes the last row. Now, at runtime the table consists of only one row and later many rows are added. So I'm thinking the DefaultTableModel is not picking up the newly added rows.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: deleting rows from a JTable

 
0
  #2
Jun 21st, 2005
Think of what you're doing and you get the idea quickly enough.
You delete row 0, then increase i to 1, then move to the next row (which is the old row 2).
Then you delete row 1, increase i to 2, move to the next row.

If you want to do something like this, start at the end and delete from there to the front.
Something like
  1. int numRows == dtm.getRowCount();
  2. for (int i=numRows;i>=0;i++) {
  3. dtm.removeElement(i);
  4. mortgageTable.revalidate();
  5. }
should give you the results you are looking for.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: deleting rows from a JTable

 
0
  #3
Jun 21st, 2005
WOW! I would have NEVER spoted that. I was thinking since it ran at runtime with only one row that it never updated the count. Thank you so much; I was banging my head against the wall on this one...I wish I could give you a big hug for that. :lol:
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: deleting rows from a JTable

 
0
  #4
Jun 22nd, 2005
I've banged my head against the wall more than once over the same problem, but finally the solution stuck in my head
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: muhdazwa is an unknown quantity at this point 
Solved Threads: 0
muhdazwa muhdazwa is offline Offline
Newbie Poster

Re: deleting rows from a JTable

 
0
  #5
May 22nd, 2008
Hi,

Hope this code is simple enough:

  1. while (mortgageTable.getRowCount() > 0) {
  2. ((DefaultTableModel) mortgageTable.getModel()).removeRow(0);
  3. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: deleting rows from a JTable

 
0
  #6
May 22nd, 2008
Original post was three years ago. I doubt they still need help. Read the dates before posting.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: muhdazwa is an unknown quantity at this point 
Solved Threads: 0
muhdazwa muhdazwa is offline Offline
Newbie Poster

Re: deleting rows from a JTable

 
0
  #7
May 22nd, 2008
Hi' thanks for reminding the date.

For me, knowledge can never be rotten. Each day there will be someone seeking better way to programs. Maybe they stumble daniweb and appreciate what we share here despite how old the codes is .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: topumirza is an unknown quantity at this point 
Solved Threads: 0
topumirza topumirza is offline Offline
Newbie Poster

Re: deleting rows from a JTable

 
0
  #8
Nov 15th, 2008
Originally Posted by jasimp View Post
Original post was three years ago. I doubt they still need help. Read the dates before posting.
public synchronized void clearTable(){
int numrows = tblSales.getRowCount();
for(int i = numrows - 1; i >=0; i--){
modelTblsales.removeRow(i);
}
}
Last edited by topumirza; Nov 15th, 2008 at 4:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: gregsci is an unknown quantity at this point 
Solved Threads: 0
gregsci gregsci is offline Offline
Newbie Poster

Re: deleting rows from a JTable

 
0
  #9
Dec 11th, 2008
This is more efficient way:
  1. //deletes ALL the rows
  2. DefaultTableModel.getDataVector().removeAllElements();
  3. //repaints the table and notify all listeners (only once!)
  4. DefaultTableModel.fireTableDataChanged();

If you do this
  1. while (mortgageTable.getRowCount() > 0) {
  2. ((DefaultTableModel) mortgageTable.getModel()).removeRow(0);
  3. }

This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: deleting rows from a JTable

 
0
  #10
Dec 12th, 2008
Originally Posted by gregsci View Post
This is more efficient way:
  1. //deletes ALL the rows
  2. DefaultTableModel.getDataVector().removeAllElements();
  3. //repaints the table and notify all listeners (only once!)
  4. DefaultTableModel.fireTableDataChanged();

If you do this
  1. while (mortgageTable.getRowCount() > 0) {
  2. ((DefaultTableModel) mortgageTable.getModel()).removeRow(0);
  3. }

This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........
let me guess ... your clock stopped in the past too?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 19191 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC