enterNestedEventLoop as public API?

John Hendrikx hjohn at xs4all.nl
Wed Nov 13 08:43:45 PST 2013


On 13/11/2013 17:27, Tom Schindl wrote:
> Currently the API requires to pass the same object to enter and exit how would your code work with multiple nested event loops?
>
> Tom

Dialog d = new Dialog() {
    public void onClose() {
       Platform.exitNestedEventLoop(d, null);
    }
}
Platform.enterNestedEventLoop(d);

An example with multiple gets complicated quick, but not due to the 
nested event loop, just hard to show in a single example, normally 
nesting dialogs would just use another instance that handles these 
details -- in my code that uses the nested event loops, there is no 
difference between calling the first dialog or a deeply nested dialog -- 
caller code is unaware of how deep the nesting goes (I can post that if 
you like, it is fully functional apart from using non-public API):

Dialog d = new Dialog("Do you want exit the program?") {
    public void onExitClick() {
      Dialog e = new Dialog("Are you  absolutely sure?") {
         public void onYesClick() {
            Platform.exitNestedEventLoop(e, true);
         }
         public void onNoClick() {
            Platform.exitNestedEventLoop(e, false);
         }
      }
       e.show();
       if(Platform.enterNestedEventLoop(e)) {
         close();  // closes first dialog, if yes was selected in 
confirmation dialog
       }
    }

    public void onCancelClick() {
       close();  // closes first dialog on cancel
    }

    public void close() {
       Platform.exitNestedEventLoop(d);
    }
}
d.show();
Platform.enterNestedEventLoop(d);

--John


> Von meinem iPhone gesendet
>
>> Am 13.11.2013 um 16:35 schrieb Stephen F Northover<steve.x.northover at oracle.com>:
>>
>> What is the difference?
>>
>> Dialog d = new Dialog() {
>>    public void onClose() {
>>       Platform.exitNestedEventLoop();
>>    }
>> }
>> Platform.enterNestedEventLoop();
>>
>>
>> Steve
>>
>>> On 2013-11-13 5:28 AM, Tom Schindl wrote:
>>> What bothers me with the API as it is today is that I have call
>>> enter/exit, I would find it more easy to work with an API like:
>>>
>>> -------8<-------
>>> WaitCondition c = new WaitCondition();
>>> Dialog d = new Dialog() {
>>>     public void onClose() {
>>>        c.release();
>>>     }
>>> }
>>> Platform.spinNestedEventLoop(c);
>>> ------->8-------
>>>
>>> Tom
>>>
>>>> On 13.11.13 11:18, Artem Ananiev wrote:
>>>> I also think it's a good request for public API. In AWT/Swing, people
>>>> had been using ugly workarounds with modal dialogs just to enter a
>>>> nested event loop, until public java.awt API was finally provided:
>>>>
>>>> http://docs.oracle.com/javase/7/docs/api/java/awt/SecondaryLoop.html
>>>>
>>>> http://docs.oracle.com/javase/7/docs/api/java/awt/EventQueue.html#createSecondaryLoop()
>>>>
>>>>
>>>> The same is here in JavaFX: unless Toolkit.enter/exitNestedEventLoop()
>>>> is exposed at javafx.* level, people will have to workaround it by using
>>>> Stage, or calling into com.sun.javafx.*, which is not good.
>>>>
>>>> Thanks,
>>>>
>>>> Artem
>>>>
>>>>> On 11/13/2013 10:15 AM, John Hendrikx wrote:
>>>>> Hi List,
>>>>>
>>>>> Any chance that Toolkit.getToolkit().enterNestedEventLoop() will in the
>>>>> future become public API?
>>>>>
>>>>> I'm currently using this to create Dialogs based on a Pane to avoid
>>>>> creating Stages (which have the nice show and showAndWait
>>>>> functionality).  I duplicated this functionality in a Pane, allowing me
>>>>> to create Dialogs on top of existing Scenes without creating a Stage,
>>>>> and it makes use of the enterNestedEventLoop and exitNestedEventLoop
>>>>> functions in com.sun.javafx.tk.Toolkit.
>>>>>
>>>>> The reason I'm avoiding the Stages is because they donot play well with
>>>>> an application that never has the mouse or keyboard focus (my
>>>>> application is fully remote controlled) -- creating a Stage, even one to
>>>>> just show a Dialog, will cause Windows to try and attract the user's
>>>>> attention by flashing its taskbar button (for which I filed a
>>>>> bug/feature request) and this is undesired.
>>>>>
>>>>> Regards,
>>>>> John
>>>>>
>>>>> (Here's a part of the DialogPane to show and close it:)
>>>>>
>>>>>     public R showDialog(Scene scene, boolean synchronous) {
>>>>>       this.synchronous = synchronous;
>>>>>       this.scene = scene;
>>>>>       this.oldFocusOwner = scene.getFocusOwner();
>>>>>
>>>>>       Parent root = scene.getRoot();
>>>>>
>>>>>       stackPane.getChildren().add(root);
>>>>>       stackPane.getChildren().add(this);
>>>>>
>>>>>       scene.setRoot(stackPane);
>>>>>
>>>>>       requestFocus();
>>>>>
>>>>>       if(synchronous) {
>>>>>         return (R)Toolkit.getToolkit().enterNestedEventLoop(this);
>>>>>       }
>>>>>
>>>>>       return null;
>>>>>     }
>>>>>
>>>>>     public void close() {
>>>>>       Parent originalRoot = (Parent)stackPane.getChildren().remove(0);
>>>>>
>>>>>       scene.setRoot(originalRoot);
>>>>>       scene = null;
>>>>>
>>>>>       if(oldFocusOwner != null) {
>>>>>         oldFocusOwner.requestFocus();
>>>>>       }
>>>>>
>>>>>       if(synchronous) {
>>>>>         Toolkit.getToolkit().exitNestedEventLoop(this, getResult());
>>>>>       }
>>>>>     }



More information about the openjfx-dev mailing list