java 警报对话框中的 JavaFX 默认焦点按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29535395/
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-02 15:26:41  来源:igfitidea点击:

JavaFX default focused button in Alert Dialog

javajavafxjavafx-8

提问by thibault

Since jdk 8u40, I'm using the new javafx.scene.control.AlertAPI to display a confirmation dialog. In the example below, "Yes" button is focused by default instead of "No" button:

从 jdk 8u40 开始,我使用新的javafx.scene.control.AlertAPI 来显示确认对话框。在下面的示例中,“是”按钮默认为焦点,而不是“否”按钮:

public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
    final Alert alert = new Alert(alertType);
    alert.setTitle(title);
    alert.setHeaderText(header);
    alert.setContentText(content);

    alert.getButtonTypes().clear();
    alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);

    final Optional<ButtonType> result = alert.showAndWait();
    return result.get() == ButtonType.YES;
}

And I don't know how to change it.

我不知道如何改变它。

EDIT :

编辑 :

Here a screenshot of the result where "Yes" button is focused by default :

这是默认情况下“是”按钮聚焦的结果截图:

enter image description here

enter image description here

回答by crusam

I am not sure if the following is the way to usually do this, but you could change the default button by looking up the buttons and setting the default-behavior yourself:

我不确定以下是否是通常执行此操作的方法,但是您可以通过查找按钮并自己设置默认行为来更改默认按钮:

public boolean showConfirmDialog(String title, String header, String content, AlertType alertType) {
    final Alert alert = new Alert(alertType);
    alert.setTitle(title);
    alert.setHeaderText(header);
    alert.setContentText(content);

    alert.getButtonTypes().clear();
    alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);

    //Deactivate Defaultbehavior for yes-Button:
    Button yesButton = (Button) alert.getDialogPane().lookupButton( ButtonType.YES );
    yesButton.setDefaultButton( false );

    //Activate Defaultbehavior for no-Button:
    Button noButton = (Button) alert.getDialogPane().lookupButton( ButtonType.NO );
    noButton.setDefaultButton( true );

    final Optional<ButtonType> result = alert.showAndWait();
    return result.get() == ButtonType.YES;
}

回答by Sheepy

A simple function thanks to crusam:

感谢 crusam 的简单功能:

private static Alert setDefaultButton ( Alert alert, ButtonType defBtn ) {
   DialogPane pane = alert.getDialogPane();
   for ( ButtonType t : alert.getButtonTypes() )
      ( (Button) pane.lookupButton(t) ).setDefaultButton( t == defBtn );
   return alert;
}

Usage:

用法:

final Alert alert = new Alert( 
         AlertType.CONFIRMATION, "You sure?", ButtonType.YES, ButtonType.NO );
if ( setDefaultButton( alert, ButtonType.NO ).showAndWait()
         .orElse( ButtonType.NO ) == ButtonType.YES ) {
   // User selected the non-default yes button
}

回答by José Pereda

If you have a look at (private) ButtonBarSkinclass, there is a method called doButtonOrderLayout()that performs the layout of the buttons, based in some default OS behavior.

如果您查看(私有)ButtonBarSkin类,就会发现有一个方法doButtonOrderLayout()可以根据某些默认的操作系统行为来执行按钮的布局。

Inside of it, you can read this:

在其中,您可以阅读以下内容:

/* now that all buttons have been placed, we need to ensure focus is set on the correct button. [...] If so, we request focus onto this default button. */

/* 现在已经放置了所有按钮,我们需要确保焦点设置在正确的按钮上。[...] 如果是这样,我们请求关注这个默认按钮。*/

Since ButtonType.YESis the default button, it will be the one focused.

由于ButtonType.YES是默认按钮,因此它将成为焦点。

So @ymene answer is correct: you can change the default behavior and then the one focused will be NO.

所以@ymene 答案是正确的:您可以更改默认行为,然后重点将是NO.

Or you can just avoid using that method, by setting BUTTON_ORDER_NONEin the buttonOrderProperty(). Now the first button will have the focus, so you need to place first the NObutton.

或者您可以通过BUTTON_ORDER_NONEbuttonOrderProperty(). 现在第一个按钮将获得焦点,因此您需要先放置NO按钮。

alert.getButtonTypes().setAll(ButtonType.NO, ButtonType.YES);

ButtonBar buttonBar=(ButtonBar)alert.getDialogPane().lookup(".button-bar");
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);

Note that YESwill still have the default behavior: This means NOcan be selected with the space bar (focused button), while YESwill be selected if you press enter (default button).

请注意,YES仍将具有默认行为:这意味着NO可以使用空格键(焦点按钮)YES选择,而如果按 Enter(默认按钮)将被选择。

Alert dialog

Alert dialog

Or you can change also the default behavior following @crusam answer.

或者您也可以更改@crusam 回答后的默认行为。