I've an Observer TableModel which listens to some changes, performed in the database, and when so the update method below is called. My problem is, how should I prevent errors like:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.set(ArrayList.java:337)
at com.mysimpatico.memoplatform.persistenceui.MeaningsViewerTopComponent$DefaultTableModelImpl.update(MeaningsViewerTopComponent.java:108)
I don't see how SwingWorker would help. I've the long-running database task being performed in a separate thread, but this calls upon a method (database persisting) in an Observable class which notifies observers.
@Override
public void update(Observable o, final Object arg) {
final Meaning meng = (Meaning) arg;
final int row;
final boolean insert;
synchronized (mengs) {
if (mengs.contains(meng)) {
row = meng.getObjId();
mengs.set(row, meng);
insert = false;
} else {
row = mengs.size();
mengs.add(row, meng); //last
insert = true;
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
if (insert) {
fireTableRowsInserted(row, row);
} else {
fireTableRowsUpdated(row, row);
}
}
});
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}
}
}