Should I create swing elements into EDT?
I got the concurrency problems with editing non thread-safe graphics elements, but I\'m creating them, they aren\'t shown yet, a
In the Swing separable model architecture, a view component listens to its model. Because a view may respond arbitrarily to events generated by model updates, the corresponding model must also be updated on the EDT. You can mitigate latency via one of two basic approaches:
Use EventQueue.invokeLater() from a separate thread, as shown here.
Use SwingWorker to obtain the additional benefits enumerated here.
Swing Threading Policy states:
In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread. Typical Swing applications do processing in response to an event generated from a user gesture. For example, clicking on a JButton notifies all ActionListeners added to the JButton. As all events generated from a user gesture are dispatched on the event dispatching thread, most developers are not impacted by the restriction.
Where the impact lies, however, is in constructing and showing a Swing application. Calls to an application's main method, or methods in Applet, are not invoked on the event dispatching thread. As such, care must be taken to transfer control to the event dispatching thread when constructing and showing an application or applet. The preferred way to transfer control and begin working with Swing is to use invokeLater. The invokeLater method schedules a Runnable to be processed on the event dispatching thread.