简体   繁体   English

什么是JavaFX Alert OK Button的fx:id?

[英]What is fx:id of JavaFX Alert OK Button?

I use TestFX framework for test my javaFX application. 我使用TestFX框架来测试我的javaFX应用程序。 I test my application like that : 我这样测试我的应用程序:

@Test
public void shouldClickOnDeletMarkerButton() {
    FxRobot bot = new FxRobot();
    bot.robotContext();
    bot.clickOn("#deleteMarkerButton");
    bot.clickOn("javafx.scene.control.Alert.CANCEL_BUTTON"); //This doesn't work.
}

I would like him to click on the OK button of JavaFX Alert Dialogs , but I have not found a fx:id. 我希望他单击JavaFX Alert Dialogs的OK按钮,但是我没有找到fx:id。

What is fx:id of JavaFX Alert OK Button ? 什么是JavaFX Alert OK Button的fx:id?

EDIT : I solve my problem, FxRobot know "read", it is enough make this : 编辑:我解决了我的问题,FxRobot知道“读”,这足以使:

@Test
public void shouldClickOnDeletMarkerButtonWhenAnyMarkerAsBeenCreated() {
    bot.robotContext();
    bot.clickOn("#deleteMarkerButton");
    bot.clickOn("OK"); //Target text / Target Button / ...
}
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <Button text="Click me!" fx:id="button" onAction="#clickAction" BorderPane.alignment="CENTER"/>
</BorderPane>

Assigning an fx:id value to an element, as shown in the code for the Button control, creates a variable in the document's namespace, which you can refer to from elsewhere in the code. 如Button按钮的代码所示,为元素分配fx:id值会在文档的命名空间中创建一个变量,您可以从代码的其他地方引用该变量。

@KaluNight, you solution is ok but works only for English localization. @KaluNight,您的解决方案还可以,但仅适用于英语本地化。

Better to work with ids. 更好地使用ID。 Indeed Alert doesn't define fx:id for own buttons, but we can do it: 实际上Alert并没有为自己的按钮定义fx:id ,但是我们可以做到:

Button okButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
okButton.setId("my custom id");

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

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