Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does Swing component initialization have to occur on the EDT?  RSS feed

Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!