user489041 user489041 - 1 year ago 110
Java Question

Deleting all the rows in a JTable

I need to remove all the rows in my JTable.

I have tried both of the following:

* Removes all the rows in the table
public void clearTable()
DefaultTableModel dm = (DefaultTableModel) getModel();



Neither of which would remove all the rows. Any ideas?

Answer Source

The following code worked for me:

DefaultTableModel dm = (DefaultTableModel) getModel();
int rowCount = dm.getRowCount();
//Remove rows one by one from the end of the table
for (int i = rowCount - 1; i >= 0; i--) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download