简体   繁体   English

JavaFX 8 创建未修饰的 TextInputDialog,它在全屏舞台上表现为模态

[英]JavaFX 8 Create Undecorated TextInputDialog that behvaes as modal over full screen stage

I have a full screen main application stage and want to make a TextInputDialog appear on top, with a modal style (ie the dialog must be dismissed before giving other input to the stage).我有一个全屏主应用程序阶段,并希望使 TextInputDialog 出现在顶部,具有模态样式(即在向舞台提供其他输入之前必须关闭对话框)。

If I set the owner to the stage, and the style to UTILITY, then it works.如果我将所有者设置为舞台,并将样式设置为 UTILITY,则它可以工作。

But if I want to remove decoration (by setting the style to UNDECORATED) then the dialog doesn't appear at all!但是,如果我想删除装饰(通过将样式设置为 UNDECORATED),则该对话框根本不会出现! I can only get it to appear by not setting the parent.我只能通过设置父级来让它出现。

If I don't set the parent, the dialog appears on top of the stage but if I click anywhere else on the application it disappears behind the full screen stage.如果我不设置父级,对话框会出现在舞台的顶部,但如果我点击应用程序上的任何其他地方,它就会消失在全屏舞台的后面。

How do I create an undecorated dialog that has modal-like behaviour over a full-screen stage?如何创建一个在全屏舞台上具有类似模态行为的未修饰对话框?

// This works, but is decorated
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.initStyle(StageStyle.UTILITY);
dialog.showAndWait();
// This doesn't show at all (probably appears underneath the stage?)
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.showAndWait();
// This still allows clicking in the stage while dialog shown. Also causes
// "exit" of fullscreen by showing task bar when dialog is shown
TextInputDialog dialog = new TextInputDialog();
dialog.initStyle(StageStyle.UNDECORATED);
dialog.showAndWait();

I've made a complete example, which appears to be something you've tried, but isn't working.我已经做了一个完整的例子,这似乎是你尝试过的东西,但没有用。 In my case it works great though.就我而言,它虽然很好用。 The window shows full screen, and the dialog comes up in the center without window decorations.窗口显示全屏,对话框出现在没有窗口装饰的中心。

public class DialogCheck extends Application{

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("text field");
        BorderPane root = new BorderPane();
        Button b = new Button("dialog");
        root.setCenter(b);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setFullScreen(true);
        primaryStage.show();

        b.setOnAction((evt)->{ 
            TextInputDialog dialog = new TextInputDialog();
            dialog.initOwner(primaryStage);
            dialog.initStyle(StageStyle.UNDECORATED);
            dialog.showAndWait();
            });


    }

}

Note that this is with java 11 & openjfx 11 on osx.请注意,这是在 osx 上使用 java 11 和 openjfx 11。

For some added debugging:对于一些额外的调试:

dialog.setOnShown( sevt ->{
            System.out.println("showing: " + dialog.isShowing() + ", " + dialog.getX());
            TextField field = dialog.getEditor();
            field.requestFocus();
            System.out.println(field.isFocused());
        });

Also note the difference between using 'showAndWait' vs 'show'.还要注意使用“showAndWait”和“show”的区别。

Dialog.showAndWait output: Dialog.showAndWait输出:

showing: false, NaN.显示:假,NaN。
false错误的

Dialog.show output: Dialog.show输出:

showing: true, 621.0显示:真实,621.0
true真的

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

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