how to clear JTable

JavaSwing

Java Problem Overview


How can i clear the content of the JTable using Java..

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

I had to get clean table without columns. I have done folowing:

jMyTable.setModel(new DefaultTableModel());

Solution 4 - Java

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.

Solution 5 - Java

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);

Solution 6 - Java

If we use tMOdel.setRowCount(0); we can get Empty table.

DefaultTableModel tMOdel = (DefaultTableModel) jtableName.getModel();
tMOdel.setRowCount(0);

Solution 7 - Java

((DefaultTableModel)jTable3.getModel()).setNumRows(0); // delet all table row

Try This:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChamalView Question on Stackoverflow
Solution 1 - Javauser85421View Answer on Stackoverflow
Solution 2 - JavaRobe ElckersView Answer on Stackoverflow
Solution 3 - JavaBlack_ZergView Answer on Stackoverflow
Solution 4 - JavaDan CheshireView Answer on Stackoverflow
Solution 5 - JavaMalithView Answer on Stackoverflow
Solution 6 - JavaMadhuka DilhanView Answer on Stackoverflow
Solution 7 - JavaShinwar ismailView Answer on Stackoverflow