java JavaFX 如何将对话框/警报带到屏幕前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38799220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:46:56  来源:igfitidea点击:

JavaFX How to bring Dialog/Alert to the front of the screen

javajavafxjavafx-8

提问by Xiao Lin Li

I want to force an Alert to be on top of other applications. Alerts seems to be lacking a setAlwaysOnTop function.

我想强制警报位于其他应用程序之上。警报似乎缺少 setAlwaysOnTop 功能。

I have seen this post: JavaFX 2.2 Stage always on top.

我看过这篇文章:JavaFX 2.2 Stage always on top

I have tried:

我努力了:

  1. create a new stage and stage.setAlwaysOnTop(true), then alert.initOwner(stage).
  2. create a new stage and stage.initModality(Modality.APPLICATION_MODAL), then alert.initOwner(stage).
  1. 创建一个新阶段和 stage.setAlwaysOnTop(true),然后是 alert.initOwner(stage)。
  2. 创建一个新阶段和stage.initModality(Modality.APPLICATION_MODAL),然后alert.initOwner(stage)。

Does anyone know how to achieve this?

有谁知道如何实现这一目标?

Edit: I am using Java 8.

编辑:我正在使用 Java 8。

Let say I have safari opened, and it is being focused. I want to bring the Alert to the top of the screen, in front of safari, when I call its showAndWait() function.

假设我打开了 safari,它正在被聚焦。当我调用它的 showAndWait() 函数时,我想将警报带到屏幕顶部,在 safari 前面。

回答by claimoar

 Alert alert = new Alert(Alert.AlertType.WARNING, "I Warn You!", ButtonType.OK, ButtonType.CANCEL);

 Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
 stage.setAlwaysOnTop(true);
 stage.toFront(); // not sure if necessary

回答by fabian

You could "steal" the DialogPanefrom a Alertand show it in a utility Stage. For this window you can set the alwaysOnTopproperty the usual way:

您可以“窃取” DialogPaneaAlert并将其显示在实用程序中Stage。对于此窗口,您可以alwaysOnTop按照通常的方式设置属性:

Alert alert = new Alert(Alert.AlertType.WARNING, "I Warn You!", ButtonType.OK, ButtonType.CANCEL);
DialogPane root = alert.getDialogPane();

Stage dialogStage = new Stage(StageStyle.UTILITY);

for (ButtonType buttonType : root.getButtonTypes()) {
    ButtonBase button = (ButtonBase) root.lookupButton(buttonType);
    button.setOnAction(evt -> {
        root.setUserData(buttonType);
        dialogStage.close();
    });
}

// replace old scene root with placeholder to allow using root in other Scene
root.getScene().setRoot(new Group());

root.setPadding(new Insets(10, 0, 10, 0));
Scene scene = new Scene(root);

dialogStage.setScene(scene);
dialogStage.initModality(Modality.APPLICATION_MODAL);
dialogStage.setAlwaysOnTop(true);
dialogStage.setResizable(false);
dialogStage.showAndWait();
Optional<ButtonType> result = Optional.ofNullable((ButtonType) root.getUserData());
System.out.println("result: "+result.orElse(null));

回答by Robert Mugattarov

I have tried fabians and claimoars solutions and simplified them to:

我尝试过 fabians 和 claimoar 解决方案并将它们简化为:

((Stage) dialog.getDialogPane().getScene().getWindow()).setAlwaysOnTop(true);

This works in my Eclipse / JavaFX app.

这适用于我的 Eclipse/JavaFX 应用程序。

回答by ali shreef

try this

试试这个

 Alert a = new Alert(AlertType.ERROR);
        a.setTitle("Title of alert");
        a.initStyle(StageStyle.UNDECORATED);
        a.setContentText("details of message");
        a.showAndWait();

you can use this to force alert message to appear to the user if some thing wrong happen,for example when user enter the data as string and you accept data as integer. i hope that helps you.

如果发生某些错误,您可以使用它来强制向用户显示警报消息,例如,当用户以字符串形式输入数据而您以整数形式接受数据时。我希望对你有帮助。

note: i think Alert is available for javafx (jdk 1.8).

注意:我认为警报可用于 javafx(jdk 1.8)。