tags:

views:

423

answers:

2

Hi all

When I select a row in a JTable, my JTable doesn't get updated anymore. When I don't select a row, the code beneath works as expected. A row is added to the JTable.

    @Override
public void update (Observable arg0, Object arg1)
{
 if (arg0 instanceof Logger)
 {
  LogItem last = systemController.logController.getLog().getLastLogItem();
  this.history.add(last);
  this.logTableModel.addItem(last);

  this.logTable.tableChanged(new TableModelEvent(this.logTableModel));
  ((DefaultTableModel)this.logTable.getModel()).fireTableStructureChanged();
  this.logTable.repaint();
  this.logTable.doLayout();
 }

}

When I close the window and reopen it, I can see the new row.

Can someone explain me why, after selecting a row, I don't see any updates in the JTable anymore?

+3  A: 

There are a number of issues with your code. You should not be calling fireTableStructureChanged as this is related to the table structure (no of columns etc) changing not the table data itself (adding a row, changing a cell value etc). Also, you should not need to call repaint and doLayout on the table itself as this should be triggered by model changes.

I think you should have a look at How to Use Tables from the Java tutorial, specifically look at using table models, listening for data changes and firing data change events.

Mark
A: 

Is the update() call happening in the Swing event thread? If not the code will need to be wrapped with something like:

SwingUtilities.invokeLater(new Runnable() { public void run() {
  logTableModel.addItem(last);
}});
John Meagher