Reputation: 1127
How can i clear the content of the JTable using Java..
Upvotes: 69
Views: 182582
Reputation: 307
((DefaultTableModel)jTable3.getModel()).setNumRows(0); // delet all table row
Try This:
Upvotes: 1
Reputation: 43
This is the fastest and easiest way that I have found;
while (tableModel.getRowCount()>0)
{
tableModel.removeRow(0);
}
This clears the table lickety split and leaves it ready for new data.
Upvotes: 4
Reputation: 29680
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.
JTable table; … DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setRowCount(0);
If you are using other TableModel
, please check the documentation.
Upvotes: 98
Reputation: 1416
If we use tMOdel.setRowCount(0);
we can get Empty table.
DefaultTableModel tMOdel = (DefaultTableModel) jtableName.getModel();
tMOdel.setRowCount(0);
Upvotes: 1
Reputation: 401
I think you meant that you want to clear all the cells in the jTable and make it just like a new blank jTable. For an example, if your table is myTable, you can do following.
DefaultTableModel model = new DefaultTableModel();
myTable.setModel(model);
Upvotes: 1
Reputation: 87
I had to get clean table without columns. I have done folowing:
jMyTable.setModel(new DefaultTableModel());
Upvotes: 7
Reputation: 977
Basically, it depends on the TableModel that you are using for your JTable. If you are using the DefaultTableModel
then you can do it in two ways:
DefaultTableModel dm = (DefaultTableModel)table.getModel();
dm.getDataVector().removeAllElements();
dm.fireTableDataChanged(); // notifies the JTable that the model has changed
or
DefaultTableModel dm = (DefaultTableModel)table.getModel();
while(dm.getRowCount() > 0)
{
dm.removeRow(0);
}
See the JavaDoc of DefaultTableModel for more details
Upvotes: 23