Skip to content

Instantly share code, notes, and snippets.

@spalladino
Created October 27, 2011 22:15
Show Gist options
  • Save spalladino/1321067 to your computer and use it in GitHub Desktop.
Save spalladino/1321067 to your computer and use it in GitHub Desktop.
EventThreadListModel
public class EventThreadListModel extends DefaultListModel {
private static final long serialVersionUID = -4389671662282607290L;
private void invokeOnEventThread(Runnable r) {
try {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeAndWait(r);
} else {
r.run();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
protected void fireContentsChanged(final Object source, final int index0, final int index1) {
invokeOnEventThread(new Runnable() {
public void run() {
EventThreadListModel.super.fireContentsChanged(source, index0, index1);
}
});
}
@Override
protected void fireIntervalAdded(final Object source, final int index0, final int index1) {
invokeOnEventThread(new Runnable() {
public void run() {
EventThreadListModel.super.fireIntervalAdded(source, index0, index1);
}
});
}
@Override
protected void fireIntervalRemoved(final Object source, final int index0, final int index1) {
invokeOnEventThread(new Runnable() {
public void run() {
EventThreadListModel.super.fireIntervalRemoved(source, index0, index1);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment