如何从fxml中的textfield获取文本

时间:2016-12-28 18:00:26

标签: javafx textfield fxml

我是FXML的新手,无法弄清楚如何从TextField输入文字。 下面是我的来自scenebuilder和我的控制器的FMXL文件

FXML:     

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="392.0" prefWidth="559.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="validator.FXMLDocumentController">
    <children>
      <GridPane layoutX="6.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="6.0" AnchorPane.rightAnchor="-6.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="181.0" minWidth="10.0" prefWidth="132.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="381.0" minWidth="10.0" prefWidth="348.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="138.0" minWidth="10.0" prefWidth="58.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
              <Button fx:id="button" onAction="#handleButtonAction" text="Validate" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <TextField id="XMLPath" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField id="XSDPath" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField id="OutputPath" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Xml Validation Tool" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="XML File Location:" GridPane.rowIndex="1" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="XSD File Location:" GridPane.rowIndex="2" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Output File Location:" GridPane.rowIndex="3" />
         </children>
      </GridPane>
    </children>
</AnchorPane>

控制器:

public class FXMLDocumentController{

    @FXML
    private TextField XMLPath;

    @FXML
    private TextField XSDPath;

    @FXML
    private TextField OutputPath;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println(XMLPath.getText());
        System.out.println(XSDPath.getText());
        System.out.println(OutputPath.getText());
    }

}

当我单击按钮来检索文本时,它会给我一个空指针错误。我觉得我错过了一些简单的事情。

0 个答案:

没有答案