aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes invokeLater() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "invokeLater()" Watch "invokeLater()" New topic
Author

invokeLater()

T H Lim
Greenhorn

Joined: Oct 14, 2004
Posts: 28
I was writing an example to showcase the usage of SwingUtilities.invokeLater(). I came to realize this simple example (code included) I have written didn't warranty the use of invokeLater(). I do encounter there were times I needed to use invokeLater() but I forgotten where and when I used. I also understand that in a non EDP thread I should use invokeLater() but in my case I don't seem to need that and it worked fine. I was hoping anyone advise me why I don't need to use invokeLater() in this piece of code. I hope there is no bug in my showcase code.

Btw, I am using JDK 1.6 / 1.7 in Linux and Windows.

Thanks.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

TH Lim wrote:I came to realize this simple example (code included) I have written didn't warranty the use of invokeLater().


Wrong! All Swing components should be constructed and updated only on the EDT.

In your code sample, you are constructing the Swing component instances on the main Thread, and updating the JLabel's text from a background Thread. Both are in violation of Swing's single threaded rule (although the first, that of constructing Swing components on the main Thread, was earlier considered acceptable).

In a trivial example, it's expected that no ill effects will normally be seen. However, failing to respect the single threaded rule in a larger Swing application can have you scratching your head years later trying to figure out the occasional anomalous behavior.

In your example, lines 8 through 26 should be wrapped in invokeLater(...) and the calls to setText(...) at lines 42 and 50 * should be wrapped in either invokeLater(...) or invokeAndWait(...) depending on the programmer's intention.

* edit And the call to getText() at line 52


luck, db
There are no new questions, but there may be new answers.
T H Lim
Greenhorn

Joined: Oct 14, 2004
Posts: 28
Thanks for the prompt reply.

What I want to find is the reason why I don't see the error with my example, is because my example is trivia or because the Swing framework has changed? Now I have the answer.

What I don't get is why should I wrap the initial code lines 8 through 26 in invokeLater()? These codes formed the initial components for this UI application.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

TH Lim wrote:Thanks for the prompt reply.

What I want to find is the reason why I don't see the error with my example, is because my example is trivia or because the Swing framework has changed? Now I have the answer.

What I don't get is why should I wrap the initial code lines 8 through 26 in invokeLater()? These codes formed the initial components for this UI application.

You don't see anomalies because yes, this is a trivial example. Large commercial applications that failed to honor Swing's single threaded rule have shown symptoms of obscure, interrmittent visual bugs on only certain hardware/OS configurations, impossible to troubleshoot/debug because of surfacing at random intervals.

As for wrapping the code from lines 8 through 25, I already told you: All Swing components should be constructed and updated only on the EDT. To that I would add: all Swing components should be queried on the EDT, too. Except repaint() [which queues a painting event on the EDT] and a very few other Swing methods, all code dealing with a Swing component should be on the EDT.

The JVM launches the main(...) method on (d'oh) the Main Thread. Not on the EDT.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
an article worth a read

http://bitguru.wordpress.com/2007/03/21/will-the-real-swing-single-threading-rule-please-stand-up/
T H Lim
Greenhorn

Joined: Oct 14, 2004
Posts: 28
I am glad I asked. Your replies strengthen my understanding on Swing. Thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invokeLater()