This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Which bits to call on Event Dispatch Thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Which bits to call on Event Dispatch Thread?" Watch "Which bits to call on Event Dispatch Thread?" New topic
Author

Which bits to call on Event Dispatch Thread?

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I have a convenience method to test out JPanels:
I'm not sure exactly which of these statements I should be putting within the inner class, and does it matter? Is it just the setVisible bit that will determine if user interactions will occur on the EDT?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

All event dispatch in Swing occurs on the EDT. As a general rule, you should run all code that constructs and alters the state of Swing components only on the EDT. Also note that various Swing methods (particularly text component methods) that are, as of Java 1.6, documented as being thread safe are not actually so, and the mention of thread safety is removed from the documentation for Java 1.7.

Further reading, even if the articles are a little dated:
Threads and Swing
The Last Word in Swing Threads


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Darryl Burke wrote:All event dispatch in Swing occurs on the EDT. As a general rule, you should run all code that constructs and alters the state of Swing components only on the EDT.

I would also add all code that queries the state of Swing components, like calling getText() on a text field.

Luigi, you can get away with creating your user interface before the EDT is started. In your example the call to SwingUtilities.invokeLater could have been omitted; the setVisible(true) method would cause the EDT to be started. However, as Darryl said, could does not mean should. Although I still go the lazy way and skip EventQueue.invokeLater* when constructing my GUIs a lot of the time.

*SwingUtilities.invokeLater delegates to EventQueue.invokeLater, as is also mentioned in the Javadocs.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Thanks. So really I should only ever call such a method from the EDT, or I should put everything in this method into the inner class, plus instantiating the JPanel I want to display. The latter could be a bit tricky if I'm subclassing JPanel, because I'd need to pass in a Class argument, rather than an instance of a panel.

Rob - thanks for the tip. I had a poke around in the JComponent.setVisible source code and it seems that the reValidate method that it calls checks whether it's being run in the EDT, and if not, adds itself.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Rob Spoor wrote:setVisible(true) method would cause the EDT to be started.

How and when the EDT is started may be platform dependent. Here's a SSCCE that demonstrates that the mere construction of a JPanel kicks off the EDT, on Windows at least.If you comment out new JPanel() the main thread terminates and the program exits before the Timer has a chance to trigger its actionPerformed.

Incidentally, I found that constructing a JComponent doesn't kick off the EDT
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Didn't work on a Linux box.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I tried it in Win XP. A JPanel keeps the application going for 3 "Timer event"s but a JComboBox only for 2!

Out of interest, why does the JVM shut down when we still have this Timer thread running? Aren't all threads supposed to terminate before it exits?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Daemon threads will never keep a JVM alive.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Interesting. On my Win7 machine, constructing either a JPanel or a JComboBox keeps the Timer running ad infinitum.

Shall try it on WinXP at the office, later today.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I also tried it on Win 7 64-bit and it carries on indefinitely on that - different to to Win XP. Weird!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which bits to call on Event Dispatch Thread?