U will have a ur own TableModel for keeping all the data. U can have a method for that Model which removes all the items in all cells of JTable. And on action event of desired button u can call this method of ur TableModel.
U can't say 'jtable.clear()' or something as it is rather tightly bound with the TableModel u have for the table.
Well.. I can't just create the source from scratch. I will give u an idea instead.
For every JTable there is its datamodel (i.e. TableModel). If u want to show something in JTable, u should have created a sub-class of 'AbstractTableModel'. And attached the model to the JTable.
Now say the sub-class is 'MyTableModel'.
Now this class will look similar to...
class MyTableModel extends AbstractTableModel
{
public int getColumnCount(...) ...
public int getValueAt(...) ...
....
// Here u add ur method to remove all the elements from ur internal storage
public void clear()
{
// all the code for removing all elements from this data model is here.
}
}
Now when u want to clear the JTable u know that u have attached 'MyTableModel' to JTable. So u can do following ....
MyTableModel mtm = (MyTableModel)jtable.getModel(); // Type cast it to ur class
mtm.clear(); // This will remove all data from the model
jtable.repaint(); // This will reflect the changes (empty table) on the screen
tablemodel is the model which you created for your table to add new rows. This is the shortest and fastest way of deleting all rows because what if you got thousands of rows? looping????
Bookmarks