1

I have a function which triggers with:

public void tableChanged(TableModelEvent e){...}

I got the TableModel from the TableModelEvent with:

TableModel model = (TableModel)e.getSource();

But I need the JTable to use it in a TablecellBalloonTip cosntructor. How can i get the JTable from the TableModel?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • could you be little bit concrete, are you want to dispplay tooltip on the TableCell ? or somethind else ? – mKorbel Jul 05 '11 at 08:55
  • you cant and you dont want to - read some basic article about Swing architecture to understand why not. And no: I'm not kidding - your swinging life will be miserable if you dont understand how the parts are supposed to be used ;-) – kleopatra Jul 05 '11 at 09:07

2 Answers2

5

You cannot get it directly from the event. You installed the listener to the model, not the table itself. The model does not have a reference to the table. Actually, the same model could possibly be reused by several tables. So you have to store the reference to the table somewhere else. If you have only one table, then this should work:

final JTable table = new JTable(); 
table.getModel().addTableModelListener(new TableModelListener() {
  @Override   
  public void tableChanged(TableModelEvent e) {   
    table.doSomething();
  }
 });

Otherwise, if you have more than one table, you can simply create a separate listener for each of them like above.

Ilya Boyandin
  • 3,069
  • 25
  • 23
1

You need to keep the JTable instance somewhere, for later use. May be as a panel instance variable.

In MVC, the Model is not tied to a particular view or controller, hence you can not get it from the Model -- it is something very much expected.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133