无法访问从另一个FXML场景中的FXML文件上传的元素

时间:2019-06-04 02:23:10

标签: javafx textfield fxml scenebuilder gridpane

我想做类似this的事情,但要动态。

我有一个场景“ Configuration.fxml”和一个“ ControllerConfiguration.java”,我想在他的GridPane“ gp”中添加“ ESP_button.fxml”,并在修改它们后读取文本字段。 (下图)

因此,我成功添加了许多具有相同ESP_button.fxml的行,但由于出现空PointerException而无法访问它们的文本字段。

我也尝试为ESP_button创建一个加载器和一个Controller,以便从ESP_button的控制器接收TextField,但是我仍然收到NullPointerException。

这是我的代码: ConfigurationController.java

    @FXML private GridPane gp;

    ObservableList<TextField> macTextField = FXCollections.observableArrayList();
    ObservableList<TextField> xTextField = FXCollections.observableArrayList();
    ObservableList<TextField> yTextField = FXCollections.observableArrayList();
    private int nEsp = 0;

    GridPane espButton ;



    private void addEspButton(int x, int y) throws IOException {
        GridPane eb = (GridPane) FXMLLoader.load(getClass().getResource("Esp_button.fxml"));
        if(x == 0 && y == 0)
            gp.add(eb, x, y);
        else {
            gp.addRow(1);
            gp.add(eb, x, y);
        }
        /* children begins with index 1 not 0)*/
        Node result = getNodeFromGridPane(eb,5,0);

        TextField mac = (TextField) getNodeFromGridPane(eb,1,0);
        TextField X = (TextField) getNodeFromGridPane(eb,3,0);
        TextField Y = (TextField) getNodeFromGridPane(eb,5,0);

        ChangeListener<Object> listener = (MAC, Xi, Yi) -> 
        printField(mac.getText(), X.getText(), Y.getText()); 

       if(X== null) System.out.println("X is null");

    }

private static void printField(String text, String text2, String text3) {
        System.out.println("You inserted this: "+"Mac "+ text+ "X "+ text2 +" y "+text3);

    }

    private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
        for (Node node : gridPane.getChildren()) {

            Integer columnIndex = GridPane.getColumnIndex(node);
            Integer rowIndex =  GridPane.getRowIndex(node);
            if(columnIndex != null && rowIndex != null) {
            if (columnIndex == col && rowIndex == row) {

                return node;
            }
            }
        }
        return null;
    }

Configuration.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="503.0" prefWidth="631.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConfigurationController">
   <children>
      <Label alignment="CENTER" layoutX="142.0" layoutY="21.0" prefHeight="70.0" prefWidth="382.0" text="CONFIGURATION" textAlignment="CENTER" wrapText="true">
         <font>
            <Font name="System Bold" size="31.0" />
         </font></Label>
      <Button fx:id="OKButton" layoutX="260.0" layoutY="423.0" mnemonicParsing="false" onMouseClicked="#OKButtontEvent" prefHeight="31.0" prefWidth="96.0" text="OK" />
      <AnchorPane layoutX="225.0" layoutY="94.0" prefHeight="40.0" prefWidth="216.0" />
      <HBox layoutX="33.0" layoutY="89.0" prefHeight="326.0" prefWidth="563.0">
         <children>
            <Label graphicTextGap="6.0" prefHeight="40.0" prefWidth="117.0" text="Numbers of ESP:">
               <HBox.margin>
                  <Insets top="4.5" />
               </HBox.margin></Label>
            <Spinner fx:id="SpinnerBox" onMouseClicked="#getSpinnerValue" prefHeight="21.0" prefWidth="71.0">
               <opaqueInsets>
                  <Insets top="10.0" />
               </opaqueInsets>
               <HBox.margin>
                  <Insets top="8.0" />
               </HBox.margin></Spinner>
            <GridPane fx:id="gp" prefHeight="52.0" prefWidth="344.0">
              <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="176.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <HBox.margin>
                  <Insets bottom="220.0" left="30.0" />
               </HBox.margin>
            </GridPane>
         </children>
      </HBox>
      <Button layoutX="55.0" layoutY="40.0" mnemonicParsing="false" text="&lt;-" />
   </children>
</AnchorPane>

ESP_button.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane fx:id="gp" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="36.0" prefWidth="316.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ButtonController">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="0.0" prefWidth="62.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="0.0" prefWidth="155.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="10.0" prefWidth="27.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="10.0" prefWidth="30.0" />
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="811.0" minWidth="0.0" prefWidth="23.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="811.0" minWidth="10.0" prefWidth="35.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints maxHeight="113.0" minHeight="10.0" prefHeight="113.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="29.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="24.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <TextField fx:id="mac" prefHeight="29.0" prefWidth="166.0" GridPane.columnIndex="1">
         <GridPane.margin>
            <Insets right="2.0" />
         </GridPane.margin>
      </TextField>
      <Label prefHeight="22.0" prefWidth="42.0" text="MAC" />
      <Label prefHeight="21.0" prefWidth="21.0" text="X:" GridPane.columnIndex="2">
         <GridPane.margin>
            <Insets left="2.0" />
         </GridPane.margin>
      </Label>
      <Label text="Y:" GridPane.columnIndex="4">
         <GridPane.margin>
            <Insets left="2.0" />
         </GridPane.margin></Label>
      <TextField prefHeight="31.0" prefWidth="226.0" GridPane.columnIndex="3">
         <GridPane.margin>
            <Insets />
         </GridPane.margin>
      </TextField>
      <TextField prefHeight="31.0" prefWidth="226.0" GridPane.columnIndex="5">
         <GridPane.margin>
            <Insets right="2.0" />
         </GridPane.margin>
      </TextField>
   </children>
   <padding>
      <Insets left="5.0" />
   </padding>
</GridPane>


我想调用X,Y和mac textProperty()以便将它们附加到侦听器,但是我意识到X,Y和mac为空,因此无法进行处理。 EspButton:Configuration 配置:Esp_button 有人可以帮我吗?

0 个答案:

没有答案