File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTable will not clear Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable will not clear" Watch "JTable will not clear" New topic

JTable will not clear

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I have a table that is populated with data if a record is found in the DB. When a record isn't found, an exception is thrown. After displaying the exception, I want to clear the data from the table, if any.

Here is how I have my table set up:
bim = new BOMInquiryModel();
sorter = new TableSorter(bim);
cs = new CellSpan1();
table = new CSTable(cs,sorter);
- BOMInquiryModel extends DefaultTableModel
-TableSorter extends TableMap which extends AbstractTableModel.
-CellSpan1 implements an interface to span columns
-CSTable takes a CellSpan object, and a TableModel.
I have tried setting the number of rows to zero after the exception occurs, and then repainting the table as well as calling the fireTableDataChanged method, but that did not work. This is the code I've tried:

This is the code I tried:

What am I missing here?
Thanks in advance!
Abhik Sarkar
Ranch Hand

Joined: Jun 14, 2003
Posts: 61
Hi Jennifer,
I am not sure if I am correct or not, but from what you have told us, this is what I can guess...
Your table is created from the TableSorter model which extends AbstractTableModel, and not DefaultFaultTable.
However, when you catch the exception, you are casting the table's model to a DefaultTableModel. I am not sure if you would get the expected behaviour.
I would try one of the following:

Or, I would create a method in the TableSorter, say called, empty(), which would empty out the BOMInquiryModel and call fireTableDataChanged internally.
Hope this helps,
[ July 18, 2003: Message edited by: Abhik Sarkar ]
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the reply. I tried the suggestion you gave me and it did not work. However, after more tinkering around with it, I found that removing all the elements from the Vector in my model works wonderfully.
Thanks for your help!
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
TableModel (and its subclasses) holds the data of JTable. Any thing you have to deal with data, you should use TableModel only.
As you are using DefaultTableModel, use setDataVector(null, columnHeaders) on DefaultTableModel.
Another way is to set null TableModel to JTable itself.
[ August 12, 2003: Message edited by: VIJAY Yadlapati ]
I agree. Here's the link:
subject: JTable will not clear
It's not a secret anymore!