在Javafx中打开一个窗口时如何设置它以便用户无法再次打开同一个窗口?

时间:2017-12-08 12:42:07

标签: java javafx javafx-8

我尝试了很多但是找不到任何解决方案。此时打开的窗口(弹出窗口)始终位于顶部,但用户仍可以访问主窗口。它应该如何,但不应该再次打开相同的弹出窗口。

    Stage stage = new Stage();
    stage.setTitle(panelTitle);     
    stage.setScene(new Scene(root));
    stage.initModality(Modality.WINDOW_MODAL); 
    stage.setAlwaysOnTop(true);
    stage.showAndWait();

先感谢您!

2 个答案:

答案 0 :(得分:1)

正如LazerBanana所说,我会禁用打开窗口的按钮,我会在你关闭它时启用它。

Stage stage = new Stage();
button.setDisable(true);
stage.setTitle(panelTitle);     
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL); 
stage.setAlwaysOnTop(true);
stage.showAndWait();
// your logic here
button.setDisable(false);

答案 1 :(得分:0)

每次创建一个新解决方案的另一种方法是创建一个,只需设置和显示。

public class Stack extends Application {

    private final Stage popup = new Stage();

    @Override
    public void start(Stage stage) throws Exception {

        BorderPane root = new BorderPane();
        root.setPrefWidth(400);
        root.setPrefHeight(200);

        Button button = new Button("ClickMePopup");

        root.setCenter(button);

        button.setOnAction(
                event -> {
                    if (!popup.isShowing()) {
                        // you dont set modality because after the stage is set to visible second time it will throw an exception.
                        // Again depends on what you need.
                        // popup.initModality(Modality.WINDOW_MODAL);

                        // this focuses the popup and main window is not clickable
                        // popup.initOwner(stage);

                        VBox dialogVbox = new VBox(20);
                        dialogVbox.getChildren().add(new Text("Some Dialog"));
                        Scene dialogScene = new Scene(dialogVbox, 300, 200);
                        popup.setScene(dialogScene);

                        // you can actually put all above into the method called initPopup() or whatever, do it once, and just show it here or just bind the property to the button.

                        popup.show();
                    }
                });

        Scene scene = new Scene(root);

        stage.setTitle("Stack");
        stage.setScene(scene);
        stage.show();

    }

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

}

或者在点击时禁用该按钮,但如果您的弹出窗口不是由按钮驱动,或者可以从其他地方打开,那么我认为第一个想法会更好一些。取决于你需要什么。

或者只是创建自己的类并Springify它。