Check if thread is EDT is necessary?

Posted by YuppieNetworking on Stack Overflow See other posts from Stack Overflow or by YuppieNetworking
Published on 2010-04-21T14:56:16Z Indexed on 2010/04/21 15:13 UTC
Read the original article Hit count: 469

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing