警报对话框中的JavaFX默认焦点按钮 [英] JavaFX default focused button in Alert Dialog
本文介绍了警报对话框中的JavaFX默认焦点按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从jdk 8u40开始,我正在使用新的 javafx.scene.control.Alert
API来显示确认对话框。在下面的示例中,是按钮默认聚焦而不是否按钮:
Since jdk 8u40, I'm using the new javafx.scene.control.Alert
API to display a confirmation dialog. In the example below, "Yes" button is focused by default instead of "No" button:
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;
}
我不知道如何改变它。
编辑:
这里是默认情况下聚焦是按钮的结果屏幕截图:
Here a screenshot of the result where "Yes" button is focused by default :
推荐答案
我不确定以下是否通常这样做,但您可以通过查找按钮并自行设置默认行为来更改默认按钮:
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;
}
这篇关于警报对话框中的JavaFX默认焦点按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文