Java Main Thread, Event Thread And Sub Event Thread


Thành viên VIP
This article is an extension of THIS. In this article I show you how to to synchronize EventThreads and their "fired" sub-EventThreads. This scenario is frequently implemented within the MainApp (or MainThread) and its "dialogs" (SWING: JDialog, JFX: Dialog). The MainThread prepares a straightforward dialog with different JAVA components (e.g. TextField, TextArea, Button, etc.) and each component might need some starting point (or priority e.g. setting of CARET to a certain TextField or Item of a ComboBox). And that could be the potential problems of EventThreads and sub-EventThreads. The scenario could be as following:


Fig.1 shows the MainThread (JavaFX) that "prepares" the Dialog with a dynamic-extensible Dialog with TextFields for inputs. And then the cursor (i.e. the caret) should be focused on the first TextField, a ready for input (Fig.2). If this "action" is NOT implemented the user has to "click" the TextField before he can start to type. And that is for the users very inconvenient. To synchronize an EventThread (which is in preparation) and its sub-EventThread (which is the "setting Caret" and can be only executed when the Dialog started) you have to command the EventThread to run the sub-EventThread "later".

	Platform.runLater(() -> {
Similar to JavaFX you run the SwingUtilities.invokeLater() to synchronize the SWING MainThread/EventThread/sub-EventThread.


When the user finished and hits the ENTER button next TextField appears for the next input (Fig.3) and the DialogPane "adds" ONE new TextField to the Dialog.


If he hits either CANCEL (in German: Abbrechen) to annul the inputs or OK to accept them and leaves the Dialog (Fig.4)


The zipped file contains the following sources:

- The MainThread (
- The EventThread (as an API: which set an action "setFocus"
- The css.css as the Cascading Style Sheets
- The Icon "bee.gif"