Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Java Main Thread, Event Thread And Sub Event Thread

Discussion in 'Java Update' started by Joe, 25/6/19.

  1. Joe

    Joe 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:

    [​IMG]
    Fig.1

    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".

    PHP:
        Platform.runLater(() -> {
            
    txt.requestFocus();
        });
    Similar to JavaFX you run the SwingUtilities.invokeLater() to synchronize the SWING MainThread/EventThread/sub-EventThread.

    [​IMG]
    Fig.2

    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.

    [​IMG]
    Fig.3

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


    [​IMG]
    Fig.4

    The zipped file 4CDJ.zip contains the following sources:

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

    Attached Files:

Chia sẻ trang này

Loading...