简体   繁体   English

如何使用Java Scene Builder从FXML获取值

[英]how to get values from a fxml using java scene builder

I am new in Javafx. 我是Javafx的新手。 I am using Java scene builder to create a form. 我正在使用Java Scene Builder创建表单。 When I saved it, it creates a fxml file. 当我保存它时,它将创建一个fxml文件。 This is the code of my form.. 这是我表格的代码。

<?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. 现在,我想获取这些字段的值,以便可以将其放入数据库中。

If you are actually using SceneBuilder, you can set the fx:id within it shown here . 如果您在实际使用SceneBuilder,您可以设置fx:id中显示它在这里 Then select the Controller Class which will be instantiated if you build the FXML (mine is de.thatsich.bachelor.javafx.DisplayPresenter in this example) 然后选择Controller类,如果您构建FXML,则将实例化该类(在本示例中为de.thatsich.bachelor.javafx.DisplayPresenter

In this controller you should have a @FXML field which has exactly the same name as the fx:id you provided in your FXML -File. 在此控制器中,您应该有一个@FXML字段,其名称与您在FXML -File中提供的fx:id 完全相同 These fields can be private if you like. 如果您愿意,这些字段可以是私有的。

For example: 例如:

I have this Button : the corresponding code for this button and its onClickAction would be 我有这个Button :该按钮及其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. 如您所见,您不需要自己实例化Button。 The FXMLLoader will do this for you. FXMLLoader将为您完成此任务。

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();。

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

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