1

I've sub classed AbstractTableModel for use as the model for my JTable. Whenever data is added to I call fireTableRowsInserted() in the AWT thread. All access to my underlying container is made thread safe by using synchronized methods.

This pattern has been working fine for me so far. However now I want to remove data from the list I've realized I have a threading issue. If I remove a row and call fireTableRowsDeleted() in the AWT thread I can still get a call to getValueAt() for a row index that now no longer exists.

What is best practice for performing operation on a the table model outside the AWT thread?

Lahiru Ashan
  • 767
  • 9
  • 16
CodeBuddy
  • 5,749
  • 1
  • 25
  • 30
  • that's depends if (overide) methods are inside AbstractTableModel and if methods FireXxxXxx and called from outside Model or not, because I don't see any difference between addRow and removeRow on both sides (Model and View too) – mKorbel Jul 12 '11 at 22:03

2 Answers2

6

The best practice IMHO is to avoid doing it. Wrap every access to the model from another thread inside a Runnable and use SwingUtilities.invokeLater to update the model.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

You'll have to remove the data from the EDT. Any operation that results in AbstractTableModel firing a change event needs to be done in the EDT.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257