This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Event dispatch thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event dispatch thread" Watch "Event dispatch thread" New topic
Author

Event dispatch thread

budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
Have a quick question : If i run something like :


Then would any action event and the swing code would run inside the EventDispatch thread or do we have to use the


then the code would run inside the eventdispatch thread ?
I tried reading some articles but not really clear with threads .
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

I assume you are referring to code you would put in your TestFr constructor. No, this will be run by the main thread, not by the EDT.

However, this is not a problem. As long as you haven't started up the EDT (which you do through the first call to pack() or setVisible() on a JFrame) you can call any methods on your Swing components. You don't have to worry about deadlock, because the EDT isn't there to hold resources yet.

After your frame becomes visible (or you call pack()) you have to start calling Swing component methods through the EDT. This mostly happens naturally through event listeners. Usually, there isn't a cause to call the invokeAndWait() or invokeLater() methods.
budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
@ Stephen Thanks for the reply . Let's assume that i do call the setVisible() method . Then would the EDT be responsible for any further executions or calculations in the TestFr .

And another(final) question is that let's say i put my code of TestFr in the eventqueue and the code starts running a lengthy calculation and then the user tries to cancel the application . Then will that cancel request be queued in the EventQueue or would be handled seperately ?? Thanks for your time .
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

budsy remo wrote:Let's assume that i do call the setVisible() method . Then would the EDT be responsible for any further executions or calculations in the TestFr .

Not all executions in your TestFr have to be called on the EDT. Just those that involve Swing/AWT components (including operations on your frame itself).

And another(final) question is that let's say i put my code of TestFr in the eventqueue and the code starts running a lengthy calculation and then the user tries to cancel the application . Then will that cancel request be queued in the EventQueue or would be handled seperately ?? Thanks for your time .


Depends on what you mean by 'cancel'. If you mean externally shutting down the application through a task manager or through the console, or by calling System.exit(), then the application will terminate immediately. If you attempt to close the application through the system menu or the close button, this will trigger an event that ends up on the EDT, which will of course only be handled after the lengthy operation (and others) have finished.

You should never call long lasting operations on the EDT, because this makes your application unresponsive. They should be handled by SwingWorkers instead.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
However, this is not a problem. As long as you haven't started up the EDT (which you do through the first call to pack() or setVisible() on a JFrame) you can call any methods on your Swing components. You don't have to worry about deadlock, because the EDT isn't there to hold resources yet.


Sun/Oracle's newer recommendation is that ALL code the create GUI components or interacts with the GUI should be done on the EDT. This includes the creation of the frame and its components even before a pack() method is invoked. Take a look at all the examples is the Swing tutorial.

http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Also, and tangential to this discussion, many of the Swing methods presently documented as being thread-safe will no longer be so documented in JDK7, It has been accepted that those methods in fact are not thread-safe, never were thread-safe and it may be considered a documentation bug.

I'm not sure, but I think in JDK7 there won't be any Swing methods documented as thread-safe.


luck, db
There are no new questions, but there may be new answers.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

Thanks Rob, Darryl
budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
@ Darryl ,Stephen and Rob ...thanks a million.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event dispatch thread
 
Similar Threads
ThreadGroup
thread woes
refer from within thread to the parent object
Threads
Stopping and starting thread for multiple key clicks