I have a couple of questions with regards to Swing and using EDT for GUI updates. I just started reading on this stuff so I am a full beginner in this area:
SwingUtilities.invokeLater
we enqueue it to the current queue of GUI update tasks (the EDT) right? How exactly does the SwingWorker ensure that done()
method is run on EDT? It sets the following code:
future = new FutureTask<T>(callable) {
@Override
protected void done() {
doneEDT();
setState(StateValue.DONE);
}
};
so I was wondering whether FutureTask somehow makes sure that invokeLater
is called?
Thanks for all your answers.
A good rule is that all operations (access/updates/...) should happen on the EDT. There are a few exceptions mentioned in the javadoc (certain methods of certain classes), but they are so hard to remember that it is easier to stick to the 'do everything on the EDT' approach. Exceptions will not be raised (luckily, JavaFX fixed this shortcoming). You can use a custom RepaintManager
to detect most of these violations: see this article.
Everything triggered by the user is handled on the EDT. For example if the user clicks on a button, the actionPerformed
of the corresponding Action
or ActionListener
will be called on the EDT.
Correct
The thing you schedule first will be executed first. The invokeLater
call simply adds the Runnable
at the end of the queue. Using invokeLater
a second time a bit later will add this new Runnable
after the previously scheduled Runnable
.
Take a look at the code for doneEDT
private void doneEDT() {
Runnable doDone =
new Runnable() {
public void run() {
done();
}
};
if (SwingUtilities.isEventDispatchThread()) {
doDone.run();
} else {
doSubmit.add(doDone);
}
}