简体   繁体   English

从任务中关闭JavaFx阶段

[英]Close Stage in JavaFx from task

I am trying to close a Stage in JavaFX from a task (thread). 我正在尝试从任务(线程)关闭JavaFX中的Stage。

In order to accomplish this, I tried to pass the references to the Stage to the a class that extends Task, setting the current Stage there. 为了完成此任务,我尝试将对Stage的引用传递给扩展Task的类,并在其中设置当前Stage。

Then closing the Stage when the call() is over. 然后在call()结束时关闭舞台。 But .close() and .hide() didn't hide/close the Stage at all. 但是.close()和.hide()根本没有隐藏/关闭舞台

Class: SampleStage 类:SampleStage

public class SampleStage extends Application {
    @Override
    public void start(final Stage primaryStage) throws Exception {
        primaryStage.setTitle("JavaFx Dialog");
        final Button btn = new Button();
        btn.setText("Click me to display popup dialog");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                Stage dialog = new Stage();
                Taskee task = new Taskee();
                dialog.initStyle(StageStyle.UTILITY);
                task.setStage(dialog);
                new Thread(task).start();
                Scene scene2 = new Scene(new Group(new Text(25, 25, "Hello World!")));
                dialog.setScene(scene2);
                dialog.show();
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();

    }
    public static void main(String[] args) {
        launch(args);
    }
}

Class Taskee: 班级任务负责人:

import javafx.concurrent.Task; 导入javafx.concurrent.Task; import javafx.stage.Stage; 导入javafx.stage.Stage;

public class Taskee extends Task<Void>{
    private Stage stage;
    @Override
    protected Void call() throws Exception {
        for(int i=0;i<10;i++){
            //@DoSomething()
            for(long l=0;l<10000;l++);
            System.out.println("i=" + i);
        }
        getStage().close();
        getStage().hide();
        return null;
    }
    public Stage getStage() {
        return stage;
    }
    public void setStage(Stage stage) {
        this.stage = stage;
    }
}

Note: getStage().getScene().getWindow().hide(); 注意: getStage().getScene().getWindow().hide(); doesn't work either. 也不起作用。

The hide() method must be called on the FX Application thread. hide()方法必须在FX Application线程上调用。 (In Java 8, your code would actually throw an exception.) (在Java 8中,您的代码实际上会引发异常。)

Use the Task 's setOnSucceeded() handler to close the stage in a situation like this: 在以下情况下,使用TasksetOnSucceeded()处理程序关闭舞台:

public class Taskee extends Task<Void>{

    private Stage stage;

    public Taskee() {
        setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent event) {
                if (stage != null) {
                    stage.hide();
                }
            }
        });
    }
    @Override
    protected Void call() throws Exception {
        for(int i=0;i<10;i++){
            //@DoSomething()
            for(long l=0;l<10000;l++);
            System.out.println("i=" + i);
        }
        return null;
    }
    public Stage getStage() {
        return stage;
    }
    public void setStage(Stage stage) {
        this.stage = stage;
    }
}

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

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