简体   繁体   English

任务中的Java FX新模式窗口

[英]Java FX new modal Window in Task

My problem is that I can not start a new modal window in Task, it just does not go out. 我的问题是我无法在Task中启动新的模态窗口,只是无法熄灭。 I do not understand how it is possible to derive not just an allert, but any modal window from Task. 我不明白如何从Task派生一个Allert以及任何模式窗口。 Translated by Google =) 由Google翻译=)

Task<Void> task = new Task<Void>() {
            @Override 
            public Void call() throws ApiException,ClientException,InterruptedException {
                 int i = 0;
                 for ( i = 0; i < bufferLenght; i++){
                    try {
                    ...some code
                    }catch(ApiCaptchaException e) {
                       ...get capcha
                       captchaSid = e.getSid();
                       captchaImg = e.getImage();
                       System.out.println( captchaSid);
}

                    System.out.println(captchaSid);
                    if (captchaSid != null) {
                        System.out.println("gg");
                        Alert alert = new Alert(AlertType.INFORMATION);
                        alert.setTitle("Test Connection");
                        //here he is stuck
                        alert.setHeaderText("Results:");
                        alert.setContentText("Connect to the database successfully!");
                        alert.showAndWait();

                        System.out.println("gg3");  
                    if(i<bufferLenght-1) {
                    Thread.sleep(2000);
                    }
                }
                return null;
            }
        };

        new Thread(task).start();

You must create and show new windows on the FX Application Thread. 您必须在FX Application Thread上创建并显示新窗口。 You can schedule code to execute on the FX Application Thread by submitting it to Platform.runLater(...) . 您可以通过将代码提交到Platform.runLater(...)来计划在FX Application Thread上执行的代码。 If you need your background thread to wait for the user to dismiss the Alert , you can use a CompletableFuture , as in this question : 如果您需要后台线程来等待用户关闭Alert ,则可以使用CompletableFuture ,如以下问题所示

Task<Void> task = new Task<Void>() {

    @Override
    protected Void call() throws Exception {

        // ...

        CompletableFuture.runAsync(() -> {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Test Connection");
            alert.setHeaderText("Results:");
            alert.setContentText("Connect to the database successfully!");
            alert.showAndWait();
        }, Platform::runLater).join();

        // ...
    }
};

If your alert is returning a value which you need, use supplyAsync(...) instead and return the value from the lambda expression. 如果您的警报返回的是您需要的值,请改用supplyAsync(...)并从lambda表达式中返回该值。 You can then assign that value to the result of join() : 然后可以将该值分配给join()的结果:

Task<Void> task = new Task<Void>() {

    @Override
    protected Void call() throws Exception {

        // ...

        String result = CompletableFuture.supplyAsync(() -> {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Test Connection");
            alert.setHeaderText("Results:");
            alert.setContentText("Connect to the database successfully!");
            alert.showAndWait();
            // presumably here you want to return a string
            // depending on the alert...
            return "" ;
        }, Platform::runLater).join();

        // ...
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM