I want to delete all the rows of DefaultTable. I found two common ways to delete them on internet, but none of them works in my case because those methods does not exist in my DefaultTableModel. I wonder why. My code for using DefaultTableModel is
DefaultTableModel Table = (DefaultTableModel) Table.getModel();
One way to delete is
Table.removeRow(Table.getRowCount() - 1);
but this removerow method does not exist in my DefaultTableModel.
removeRow(Table. getRowCount() - 1);
You must remove the data from the TableModel used for the table. If using the DefaultTableModel , just set the row count to zero. This will delete the rows and fire the TableModelEvent to update the GUI.
Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method. The first index in the Object[][] array is the row index and the second is the column index.
You can set the row count to 0.
setRowCount(0)
Quote from documentation:
public void setRowCount(int rowCount)
Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded.
But as you can't find removeRow
either I suspect you haven't typed you model variable as DefaultTableModel
perhaps, maybe just TableModel
?
In that case cast your TableModel
to DefaultTableModel
like this:
DefaultTableModel model = (DefaultTableModel) table.getModel();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With