6

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.

fabian
  • 67,623
  • 12
  • 74
  • 102
  • Is it really required to 'pause' within the method execution? Just finish the method instead of calling 'pause()' and do the rest of the work in the resume button's action handler. – isnot2bad Sep 26 '17 at 11:42

2 Answers2

7

You can do so by using Platform.enterNestedEventLoop to pause the execution of the event handler and Platform.exitNestedEventLoop (available since JavaFX 9) to resume the execution:

private final Object PAUSE_KEY = new Object();

private void pause() {
    Platform.enterNestedEventLoop(PAUSE_KEY);
}

private void resume() {
    Platform.exitNestedEventLoop(PAUSE_KEY, null);
}

Platform.enterNestedEventLoop returns when Platform.exitNestedEventLoop is called with the same parameter passed as first argument.

fabian
  • 67,623
  • 12
  • 74
  • 102
  • Is `Platform.enterNestedEventLoop` idempotent, or do calls accumulate? Specifically, if `Platform.enterNestedEventLoop(...)` is called, and then called from the nested loop with the same parameter, does the first call to `Platform.exitNestedEventLoop` (with the same parameter) release both blocks, or does it only release the block in the nested event loop (requiring a second call to release the original block)? – James_D Sep 22 '17 at 16:30
  • 1
    @James_D It's not allowed to use the same key with `enterNestedEventLoop` more than once before the nested loop is exited. A `IllegalArgumentException` will be thrown if you try, see [javadoc: *IllegalArgumentException - if the specified key is associated with a nested event loop that has not yet returned*](http://download.java.net/java/jdk9/jfxdocs/javafx/application/Platform.html#enterNestedEventLoop-java.lang.Object-) – fabian Sep 22 '17 at 17:33
  • And before Java9? :) – user1803551 Oct 17 '17 at 02:02
  • @user1803551 It's a javafx 9 question. There are similar methods available in previous javafx versions, but they recide in the `com.sun` packages that are not recommended to be used. – fabian Oct 17 '17 at 15:03
0

I am currently running JFX 8 where I have the similar feature in the Toolkit class.

Toolkit.getToolkit().enterNestedEventLoop(obj);

and

Toolkit.getToolkit().exitNestedEventLoop(obj);

Have not looked at the JFX 9 source, but my bet is that the Platform methods are simply shortcuts to the same.

Daniel B
  • 1,055
  • 10
  • 17
  • 1
    It's likely a shortcut, but always remember: Don't use private API for production because they have every single right to remove a private API and replace with something else, without telling anyone about the change. So at any point they can remove `Toolkit.getToolkit().enterNestedEventLoop(Object)` and make `Platform.enterNestedEventLoop()` call some other private stuff that does the same thing. – Jai Jun 12 '18 at 01:06