Package swingtree.threading
Interface EventProcessor
- All Known Implementing Classes:
DecoupledEventProcessor
public interface EventProcessor
One of Swing's biggest drawback is that it is single threaded. This means that all
GUI events are processed on the same thread as the application logic.
This is problematic for several reasons:
See
This is problematic for several reasons:
- The GUI is not responsive while the application logic is running. For example, if you have a long running task, the GUI will not be updated until the task is finished.
- The GUI is not thread safe. If you try to update the GUI from a different thread than the GUI thread, you will get an exception.
- The GUI is not decoupled from the application logic. This means that the GUI is tightly coupled to the application logic, which makes it hard to test the GUI.
- Using your GUI thread not only for GUI events and rendering but also for application logic, will reduce the performance of your application.
EventProcessor
interface.
If you want to do proper decoupling, you can switch to the decoupled event processor like so:
use(EventProcessor.DECOUPLED, ()->
UI.panel("fill")
.add( "shrink", UI.label( "Username:" ) )
.add( "grow, pushx", UI.textField("User1234..42") )
.add( label( "Password:" ) )
.add( "grow, pushx", UI.passwordField("child-birthday") )
.add( "span",
UI.button("Login!").onClick( it -> {...} )
)
);
Note that by default, Swing-Tree uses a simple event processor which does not delegate GUI events to a separate thread.
So if you want to activate this powerful feature you have to change the event processor. See
COUPLED
, COUPLED_STRICT
and DECOUPLED
.-
Field Summary
Modifier and TypeFieldDescriptionstatic final EventProcessor
This event processor simply runs the events immediately without caring about the thread they are executed on.static final EventProcessor
This event processor runs the events immediately on the GUI thread.static final DecoupledEventProcessor
This event processor makes a distinction between application events and UI events. -
Method Summary
Modifier and TypeMethodDescriptionvoid
registerAndRunAppEventNow
(Runnable runnable) Adds the supplied task to an event queue for processing application events and then waits for the task to be processed on the application thread.void
registerAndRunUIEventNow
(Runnable runnable) Adds the supplied task to an event queue for processing UI events and then waits for the task to be processed on the GUI thread.void
registerAppEvent
(Runnable runnable) Adds the supplied task to an event queue for processing application events.void
registerUIEvent
(Runnable runnable) Adds the supplied task to an event queue for processing UI events.
-
Field Details
-
COUPLED
This event processor simply runs the events immediately without caring about the thread they are executed on. This means that events are executed on the GUI thread, which is the EDT in Swing. However, events may also be executed on other threads, which is why this event processor is usually used for testing. Make sure an application thread is registered throughDecoupledEventProcessor.join()
atDECOUPLED
when using this processor, otherwise events will not be handled. -
COUPLED_STRICT
This event processor runs the events immediately on the GUI thread. If the current thread is the GUI thread, the events are executed immediately, otherwise an exception is thrown, and it's stack trace is printed (but the application will not crash). -
DECOUPLED
This event processor makes a distinction between application events and UI events. Application events are executed on the application thread, whereas UI events are executed on the GUI thread (AWT Event Dispatch Thread).
-
-
Method Details
-
registerAppEvent
Adds the supplied task to an event queue for processing application events. The task is executed in the application thread, which may be the GUI thread (but shouldn't be if you are doing clean software development). This method returns immediately.- Parameters:
runnable
- The task to be executed in the application thread.
-
registerAndRunAppEventNow
Adds the supplied task to an event queue for processing application events and then waits for the task to be processed on the application thread. The task is executed in the application thread, which, depending on your implementation, may also be the GUI thread (but shouldn't be if you are doing clean software development). This method returns when the task has been processed.- Parameters:
runnable
- The task to be executed in the application thread.
-
registerUIEvent
Adds the supplied task to an event queue for processing UI events. The task is executed in the GUI thread, which is the EDT in Swing. This method returns immediately.- Parameters:
runnable
- The task to be executed in the GUI thread.
-
registerAndRunUIEventNow
Adds the supplied task to an event queue for processing UI events and then waits for the task to be processed on the GUI thread. The task is executed in the GUI thread, which is the EDT in Swing. This method returns when the task has been processed.- Parameters:
runnable
- The task to be executed in the GUI thread.
-