| | |
deleting rows from a JTable
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I know that you can call this method to delete a row from a JTable:
I need to delete all of the rows at some point, and this is not working:
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.
Java Syntax (Toggle Plain Text)
DefaultTableModel.deleteRow(index);
I need to delete all of the rows at some point, and this is not working:
Java Syntax (Toggle Plain Text)
for (int i=0; i<dtm.getRowCount(); i++) { dtm.removeRow(i); mortgageTable.revalidate(); }
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.
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
should give you the results you are looking for.
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
Java Syntax (Toggle Plain Text)
int numRows == dtm.getRowCount(); for (int i=numRows;i>=0;i++) { dtm.removeElement(i); mortgageTable.revalidate(); }
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: May 2008
Posts: 3
Reputation:
Solved Threads: 0
Hi,
Hope this code is simple enough:
Hope this code is simple enough:
Java Syntax (Toggle Plain Text)
while (mortgageTable.getRowCount() > 0) { ((DefaultTableModel) mortgageTable.getModel()).removeRow(0); }
•
•
Join Date: Oct 2008
Posts: 1
Reputation:
Solved Threads: 0
Last edited by topumirza; Nov 15th, 2008 at 4:49 pm.
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
This is more efficient way:
If you do this
This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........
Java Syntax (Toggle Plain Text)
//deletes ALL the rows DefaultTableModel.getDataVector().removeAllElements(); //repaints the table and notify all listeners (only once!) DefaultTableModel.fireTableDataChanged();
If you do this
Java Syntax (Toggle Plain Text)
while (mortgageTable.getRowCount() > 0) { ((DefaultTableModel) mortgageTable.getModel()).removeRow(0); }
This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........
•
•
•
•
This is more efficient way:
Java Syntax (Toggle Plain Text)
//deletes ALL the rows DefaultTableModel.getDataVector().removeAllElements(); //repaints the table and notify all listeners (only once!) DefaultTableModel.fireTableDataChanged();
If you do this
Java Syntax (Toggle Plain Text)
while (mortgageTable.getRowCount() > 0) { ((DefaultTableModel) mortgageTable.getModel()).removeRow(0); }
This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........
![]() |
Similar Threads
- Adding a deleting rows (JSP)
- Deleting Rows in excel (Visual Basic 4 / 5 / 6)
- Delete from multiple tables (MySQL)
- Use of DataGrid (ASP.NET)
Other Threads in the Java Forum
- Previous Thread: Getting integer values from a JTable
- Next Thread: Hashtable validation with javascript
Views: 19191 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






