简体   繁体   中英

How to make a new Stage in Java-fx and how to make that stage as pop-up?

I want to make a new stage in JavaFX and when I will click a button the new stage will pop-up with a new scene .

When the new scene will pop-up previous stage will appear but wouldn't work. When I would close the pop-up then the previous window will work. Please help me to do that.

You could try using the code snippet below:

public void start(Stage primaryStage) {
    try {
        Pane root = new Pane();
        Scene scene = new Scene(root, 400, 400);
        Button btn = new Button(); // create a btn
        root.getChildren().add(btn); // add this btn to the root

        btn.setOnAction(new EventHandler<ActionEvent>() // when click
        {
            @Override
            public void handle(ActionEvent e) {
                Stage dialog = new Stage(); // new stage
                dialog.initModality(Modality.APPLICATION_MODAL);
                // Defines a modal window that blocks events from being
                // delivered to any other application window.
                dialog.initOwner(primaryStage);
                VBox vb = new VBox(20);
                Scene dialogScene = new Scene(vb, 300, 200);
                dialog.setScene(dialogScene);
                dialog.show();
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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