Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear contents of a jTable ?

Tags:

java

swing

jtable

I have a jTable and it's got a table model defined like this:

javax.swing.table.TableModel dataModel = 
     new javax.swing.table.DefaultTableModel(data, columns);
tblCompounds.setModel(dataModel);

Does anyone know how I can clear its contents ? Just so it returns to an empty table ?

like image 708
tom Avatar asked Oct 07 '10 07:10

tom


People also ask

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. JTable table; …

What is the use of JTable component?

The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.


1 Answers

Easiest way:

//private TableModel dataModel;
private DefaultTableModel dataModel;


void setModel() {
  Vector data = makeData();
  Vector columns = makeColumns();
  dataModel = new DefaultTableModel(data, columns);
  table.setModel(dataModel);
}

void reset() {
  dataModel.setRowCount(0);
}

i.e. your reset method tell the model to have 0 rows of data The model will fire the appropriate data change events to the table which will rebuild itself.

like image 197
locka Avatar answered Oct 13 '22 11:10

locka