How to wait for user input on JavaFX application thread without using showAndWait?

前端 未结 2 1456
醉话见心
醉话见心 2020-12-02 02:00

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.

相关标签:
2条回答
  • 2020-12-02 02:32

    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.

    0 讨论(0)
  • 2020-12-02 02:44

    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.

    0 讨论(0)
提交回复
热议问题