aspose file tools*
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
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Does Swing component initialization have to occur on the EDT?