views:

60

answers:

2

Hi,

I have a table off AbstractTableModel. The data in the table is stored in a Vector. Now, when I remove a row, I am removing it successfully from the vector, but this is not getting updated in the view i.e. in the GUI.

I read elsewhere that I need to use fireTableRowsDeleted(), and I am trying to call it inside a wrapper method in my AbstractTableModel:

dataModel = new AbstractTableModel() {
          public void removeAl() {
                  fireTableRowsDeleted(0, getRowCount()-1);
          }
};

But, this removeAl is not accessible for me. I cannot call it like this anywhere: dataModel.removeAl()

Could you please help me understand what is going wrong? How do I update the GUI on deletion of rows?

EDIT: As it turns out, the problem was elsewhere. Once I fixed that, removing the row from the Vector itself started updating the GUI. :)

+1  A: 

You cannot see your removeAll method is because it is declared in an anonymous class - an anonymous class cannot be referred to by name, any new declarations in it cannot be accessed. For that reason, public declarations in anonymous classes are usually overrides, since they will be available by using the non-anonymous base class.

To fix this, declare your table model as a regular subclass of AbstractTableModel

public class MyTableModel extends AbstractTableModel {

   public void removeAll() {
      fireTableRowsDeleted(...);
   }
}

Your client code will then have to cast to MyTableModel to access the removeAll() method.

A simpler, but poorer solution is to continue to have clients use AbstractTableModel, and have clients explicitly call fireTableRowsDeleted().

mdma
Thanks for the reply. I will try this and let you know how it goes.
Chaitanya
A: 

In addition to what mdma said, you also need to override the following methods from TableModel interface (they are left unimplemented by AbstractTableModel), hence you can't instantiate any AbstractTableModel subclass unless it does override these methods:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

Lastly, just firing tableRowsDeleted in your removeAll() method will not be enough, you'll need to have an impact on what the 3 previous methods return; i.e. after removeAll() is called, you should make sure that getRowCount() returns 0, otherwise you'll have problems (NullPointerException or equivalent in the worst case, JTable refresh problems in the best case)!

Maybe you'd better off using DefaultTableModel in a first step, that would make it easier for you maybe. Deriving from AbstractTableModel would come later. It pretty much depends on what you wanna do actually...

jfpoilpret