Hello,
I have an UI implemented with Swing. One component does some work that may take some time, so I use SwingUtilities.invokeLater
. However, I was reading some old code and found this in an ActionListener
:
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// code X
}
});
} else {
// code X
}
I thought that it made sense since it separates code X
from the EDT. However, I found it error-prone since I have used it a couple of times and both times I forgot the else
part.
The question is: is the SwingUtilities.isEventDispatchThread()
checking necessary? Or could I assume that I am not in the EDT and always use invokeLater
?
Thanks a lot.