javamultithreadingswingjtableabstracttablemodel

Best practise for updating data in a AbstractTableModel in a non AWT thread


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?


Solution

  • 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.