从一个fxml切换到另一个fxml后,如何在JavaFX中重新填充TextField?

时间:2017-08-30 07:43:57

标签: java javafx javafx-8

我正在JavaFX中构建一个非常简单的库存管理程序,作为学校项目的一部分。我给出的一个要求是为部件提供两个不同的类,InHousePart和OutsourcedPart,它们都从一个抽象的Part类继承。除了一个字段外,这两个类是相同的:InHousePart具有“machineID”而OutsourcedPart具有“companyName”。第二个要求是有两个FXML文档,每种类型的一个部分,我需要使用一对单选按钮在它们之间切换:内部和外部。当我从一个切换到另一个时,我需要从一个到另一个重新填充公共字段,但是我似乎无法使它工作。

FXML文件是在SceneBuilder中构建的,每个字段在两个文件中都有唯一的名称。我还验证了我的Java代码和FXML正确地相互引用。

在Outsourced屏幕中,用户可以选择通过单选按钮选择In-House并更改部件类型。下面是我的字段声明以及选择此单选按钮时调用的方法。

@FXML
private RadioButton partInhouseOnInhouse;
@FXML
private TextField nameOnOutsourced;
@FXML
private TextField nameOnInhouse;
@FXML
private TextField costOnOutsourced;
@FXML
private TextField costOnInhouse;
@FXML
private TextField priceOnOutsourced;
@FXML
private TextField priceOnInhouse;
@FXML
private TextField inStockOnOutsourced;
@FXML
private TextField inStockOnInhouse;
@FXML
private TextField invMinOnOutsourced;
@FXML
private TextField invMinOnInhouse;
@FXML
private TextField invMaxOnOutsourced;
@FXML
private TextField invMaxOnInhouse;

@FXML
private void switchToInHouseFromOutsourced() throws IOException {

nameOnInhouse = nameOnOutsourced;
priceOnInhouse = priceOnOutsourced;
inStockOnInhouse = inStockOnOutsourced;
invMinOnInhouse = invMinOnOutsourced;
invMaxOnInhouse = invMaxOnOutsourced;
costOnInhouse = costOnOutsourced;

System.out.println(nameOnInhouse.getText());

Parent root = null;

partStage=(Stage) nameOnOutsourced.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("modifyPart_Inhouse.fxml"));

Scene scene = new Scene(root);
    partStage.setScene(scene);

    partStage.show();
}

这是“modifyPart_Outsourced.fxml”的FXML。 “modifyPart_Inhouse.fxml”几乎完全相同,除了fx:字段的id是根据上面的声明命名的。

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="inventory.view.modifyPartController">
   <children>
      <Text fx:id="title" layoutX="14.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Modify Part" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="14.0">
         <font>
            <Font size="16.0" />
         </font>
      </Text>
      <RadioButton fx:id="partInhouseOnOutsourced" layoutX="168.0" layoutY="15.0" mnemonicParsing="false" onAction="#switchToInHouseFromOutsourcedOnOutsourced" text="In-House">
         <toggleGroup>
            <ToggleGroup fx:id="parttype" />
         </toggleGroup>
      </RadioButton>
      <RadioButton fx:id="partOutsourceOnOutsourced" layoutX="316.0" layoutY="15.0" mnemonicParsing="false" onAction="#switchToOutsourcedFromInhouseOnInhouse" selected="true" text="Outsourced" toggleGroup="$parttype" />
      <GridPane layoutX="21.0" layoutY="47.0" prefHeight="147.0" prefWidth="461.0" AnchorPane.bottomAnchor="60.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="50.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="132.0" minWidth="10.0" prefWidth="130.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="10.0" prefWidth="170.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="116.0" minWidth="10.0" prefWidth="47.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="156.0" minWidth="10.0" prefWidth="108.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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="ID" />
            <Label text="Name" GridPane.rowIndex="1" />
            <Label text="Cost" GridPane.rowIndex="2" />
            <Label text="Price" GridPane.rowIndex="3" />
            <Label text="In Stock" GridPane.rowIndex="4" />
            <Label text="Inventory Min" GridPane.rowIndex="5" />
            <Label text="Max" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="5" />
            <Label text="Company Name" GridPane.rowIndex="6" />
            <TextField fx:id="partIDOnOutsourced" disable="true" editable="false" GridPane.columnIndex="1" />
            <TextField fx:id="nameOnOutsourced" promptText="Part Name" GridPane.columnIndex="1" GridPane.rowIndex="1">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
            <TextField fx:id="costOnOutsourced" promptText="Cost" GridPane.columnIndex="1" GridPane.rowIndex="2">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
            <TextField fx:id="priceOnOutsourced" promptText="Price" GridPane.columnIndex="1" GridPane.rowIndex="3">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
            <TextField fx:id="inStockOnOutsourced" promptText="# in stock" GridPane.columnIndex="1" GridPane.rowIndex="4">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
            <TextField fx:id="invMinOnOutsourced" promptText="Inventory Minimum" GridPane.columnIndex="1" GridPane.rowIndex="5">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
            <TextField fx:id="invMaxOnOutsourced" promptText="Inventory Maximum" GridPane.columnIndex="3" GridPane.rowIndex="5">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
            <TextField fx:id="companyName" promptText="Company Name" GridPane.columnIndex="1" GridPane.rowIndex="6">
               <font>
                  <Font name="System Italic" size="13.0" />
               </font>
            </TextField>
         </children>
      </GridPane>
      <ButtonBar layoutX="286.0" layoutY="346.0" prefHeight="40.0" prefWidth="200.0">
        <buttons>
          <Button mnemonicParsing="false" onAction="#handleSaveButton" text="Save" />
            <Button mnemonicParsing="false" onAction="#handleCancelButton" text="Cancel" />
        </buttons>
      </ButtonBar>
   </children>
</AnchorPane>

我在方法中添加了System.out.println(nameOnInhouse.getText());,以查看是否正在填充字段并且正确的数据正在输出到控制台,即使该字段为空。我也没有收到任何错误,新的fxml加载得很好,但字段总是空白的。

如果不符合要求,除了切换FXML之外,我会寻找另一种方法来做到这一点,但它就是它。

感谢任何帮助或指示。

0 个答案:

没有答案