Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all the rows of DefaultTableModel

Tags:

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.

like image 800
Xara Avatar asked May 02 '12 12:05

Xara


People also ask

How do I delete all rows in DefaultTableModel?

removeRow(Table. getRowCount() - 1);

How delete all data from JTable?

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.

What is the use of DefaultTableModel in Java?

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.


1 Answers

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();
like image 149
Mattias Isegran Bergander Avatar answered Oct 12 '22 23:10

Mattias Isegran Bergander