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 Does Swing component initialization have to occur on the EDT? 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 "Does Swing component initialization have to occur on the EDT?" Watch "Does Swing component initialization have to occur on the EDT?" New topic
Author

Does Swing component initialization have to occur on the EDT?

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
For example:



Must the initialization of a Swing component occur on the EDT or may the initialization occur on any thread? Obviously altering the components later needs to be on the EDT, showing a JFrame for example or changing the Border of a JComponent. I would have expected the initialization to be safe, but now that I encounter even more lunacy in Swing I'm not so sure. If this must occur on the EDT, how do you achieve it? I find invokeLater() unacceptable as it means the current thread can return with a reference to our created class where the members of that class have not necessarily been initialized, so I'd have to do something like this:



That would force me to make field non-final unnecessarily. The only option that would leave me with is throwing an exception if it was created outside the EDT, which may cause a lot of unnecessary headache for clients that wish to create it and constantly have to shift the creation to the EDT.

Please tell me the initialization is thread-safe at least.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Does Swing component initialization have to occur on the EDT?