Search code examples
javaswingjtabletablemodel

How to clear a custom TableModel that extends AbstractTableModel


Exactly what the question states. I created a custom tableModel class and I want to clear the JTable associated with that model on an actionPerformed.


Solution

  • 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 using other TableModel please check the documentation.