user489041 user489041 - 1 year ago 86
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--) {