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, c
You have a couple of options:
new DefaultTableModel()
, but remember to re-attach any listeners.model.removeRow(index)
to remove.clear
method.public void deleteAllRows() {
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
}
Another easy answer:
defaultTableModel.getDataVector().removeAllElements();
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 contains 40 raws, you can do following.
DefaultTableModel model = (DefaultTableModel)this.jTable.getModel();
model.setRowCount(0);
model.setRowCount(40);
One of the trivial methods is to use the following option.
dataModel.setRowCount(0);
dataModel is the model which you would like clear the content on
However, it is not optiomal solution.
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.