如何使用java场景构建器从fxml中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19024813/
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
how to get values from a fxml using java scene builder
提问by BlueShark
I am new in Javafx. I am using Java scene builder to create a form. When I saved it, it creates a fxml file. This is the code of my form..
我是 Javafx 的新手。我正在使用 Java 场景构建器来创建表单。当我保存它时,它会创建一个 fxml 文件。这是我表单的代码..
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<Label layoutX="118.0" layoutY="95.0" prefWidth="82.0" text="Username" />
<TextField layoutX="200.0" layoutY="95.0" prefWidth="143.0" />
<TextField layoutX="200.0" layoutY="137.0" prefWidth="143.0" />
<Label layoutX="118.0" layoutY="143.0" prefWidth="61.0" text="Password" />
<Button layoutX="244.0" layoutY="190.0" mnemonicParsing="false" text="submit" />
</children>
</AnchorPane>
Now I want to get the values of those fields so I can put that in database.. please any one help me.
现在我想获取这些字段的值,以便我可以将其放入数据库中。请任何人帮助我。
回答by zenbeni
Add a fx:id="myWidget" in your FXML components like <Button fx:id="myButton" .../>
在您的 FXML 组件中添加一个 fx:id="myWidget",例如 <Button fx:id="myButton" .../>
Then you add a java controller declared in your FXML and inject the component inside it with the annotation @FXML
然后添加一个在 FXML 中声明的 java 控制器,并在其中注入带有 @FXML 注释的组件
@FXML
private Button myButton;
Once you get it in java, you can access any property, for your case it should be textInput.getText();
一旦你在 java 中得到它,你就可以访问任何属性,对于你的情况,它应该是 textInput.getText();
回答by thatsIch
If you are actually using SceneBuilder, you can set the fx:id
within it shown here. Then select the Controller Class which will be instantiated if you build the FXML (mine is de.thatsich.bachelor.javafx.DisplayPresenter
in this example)
如果您实际使用的是 SceneBuilder,则可以fx:id
在此处显示的内部进行设置。然后选择如果您构建 FXML 将被实例化的控制器类(我的de.thatsich.bachelor.javafx.DisplayPresenter
在这个例子中)
In this controller you should have a @FXML
field which has exactlythe same name as the fx:id
you provided in your FXML
-File. These fields can be private if you like.
在此控制器中,您应该有一个与您在-File 中提供的名称完全相同的@FXML
字段。如果您愿意,这些字段可以是私有的。fx:id
FXML
For example:
例如:
I have this Button: the corresponding code for this button and its onClickAction would be
我有这个按钮:这个按钮及其 onClickAction 的相应代码是
DisplayPresenter.java
DisplayPresenter.java
@FXML private Button nodeButtonAddImage;
@FXML private void onAddImageAction() throws IOException {
...
}
and as you can see, you don't need to instantiate the Button yourself. The FXMLLoader will do this for you.
正如您所看到的,您不需要自己实例化 Button。FXMLLoader 会为你做这件事。