I'd like to pause the execution of a method on the JavaFX application thread and wait until the user does interaction with the UI. It's important not to freeze the UI.
Example:
Button start = ...
Button resume = ...
start.setOnAction(evt -> {
System.out.println("starting");
start.setDisable(true);
System.out.println("please press resume button.");
pause();
System.out.println("done");
start.setDisable(false);
});
resume.setOnAction(evt -> resume());
How should I implement the pause()
and resume()
methods?
The execution of the event handler should wait at pause();
call until the user presses the resume
button and the resume
method is called.
You can do so by using
Platform.enterNestedEventLoop
to pause the execution of the event handler andPlatform.exitNestedEventLoop
(available since JavaFX 9) to resume the execution:Platform.enterNestedEventLoop
returns whenPlatform.exitNestedEventLoop
is called with the same parameter passed as first argument.I am currently running JFX 8 where I have the similar feature in the Toolkit class.
and
Have not looked at the JFX 9 source, but my bet is that the Platform methods are simply shortcuts to the same.