Should I allocate/create swing elements into EDT?

前端 未结 2 1920
萌比男神i
萌比男神i 2021-01-24 12:40

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

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 13:03

    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.

提交回复
热议问题