JavaFX FXML 修改列表

JavaFX FXML modifying list

我正在做一个项目,想要有一个菜单,用户可以从中添加和删除项目。我已经看到 java 文档说在您的菜单对象上使用 getItems 但这在运行时给了我一个空点错误“由以下原因引起:java.lang.NullPointerException 在 app.Main_Controller.PopulateModelBarAdd(Main_Controller.java:257)"

    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.Main_Controller">
   <children>
      <VBox layoutX="-1.0" layoutY="39.0" prefHeight="361.0" prefWidth="270.0">
         <children>
            <HBox prefHeight="37.0" prefWidth="270.0">
               <children>
                  <TextField fx:id="VarientAdminPanelText" promptText="Varient" />
                  <Button fx:id="RemoveVarAdminPanel" mnemonicParsing="false" text="Remove" />
                  <Button fx:id="AddVarAdminPanel" mnemonicParsing="false" text="Add " onAction= "#PopulateModelBarAdd"/>
               </children>
            </HBox>
            <HBox prefHeight="33.0" prefWidth="270.0">
               <children>
                  <TextField fx:id="XCordAdminPanel" promptText="X codinate" />
                  <TextField fx:id="YCordAdminPanel" promptText="Y Cordinate" />
                  <TextField fx:id="ZCordAdminPanel" promptText="Z Cordinate" />
               </children>
            </HBox>
            <HBox prefHeight="262.0" prefWidth="270.0">
               <children>
                  <Button fx:id="AddCordAdminPanel" mnemonicParsing="false" prefHeight="25.0" prefWidth="135.0" text="Add" />
                  <Button fx:id="RemoveCordAdminPanel" mnemonicParsing="false" prefHeight="25.0" prefWidth="139.0" text="Remove" />
               </children>
            </HBox>
            <Button fx:id="AdminBack" mnemonicParsing="false" onAction="#HandelAdminVeiwMain" text="Back To Main Veiw" />
         </children>
      </VBox>
      <HBox layoutX="4.0" prefHeight="32.0" prefWidth="600.0">
         <children>
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Admin Panel">
               <font>
                  <Font name="Calibri" size="34.0" />
               </font>
            </Text>
         </children>
      </HBox>
      <VBox layoutX="279.0" prefHeight="400.0" prefWidth="321.0" />
   </children>
</AnchorPane>

菜单所在的FXML文件:

<GridPane hgap="14.0" maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" vgap="20.0" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.Main_Controller">
  <children>
    <ImageView fitHeight="61.0" fitWidth="60.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="TOP">
      <image>
        <!-- place holder -->
      </image>
    </ImageView>
    <VBox maxHeight="+Infinity" maxWidth="+Infinity" minHeight="-Infinity" prefWidth="400.0" spacing="7.0" GridPane.columnIndex="1" GridPane.rowIndex="0">
      <children>
        <Label fx:id="messageLabel" text="Remove from Buffer" textAlignment="LEFT" wrapText="true">
          <font>
            <Font name="System Bold" size="13.0" />
          </font>
        </Label>
        <Label fx:id="detailsLabel" textAlignment="LEFT" wrapText="true">
          <font>
            <Font size="12.0" />
          </font>
        </Label>
            <SplitMenuButton fx:id="RemoveVarMenu" mnemonicParsing="false" prefHeight="25.0" prefWidth="96.0" text="Model">
               <items>
                  <Menu fx:id="test" mnemonicParsing="false" text="Unspecified Menu">
                    <items>
                      <MenuItem mnemonicParsing="false" text="Action 1" />
                    </items>
                  </Menu>
                  <MenuItem mnemonicParsing="false" text="Alasdair" />
               </items></SplitMenuButton>
      </children>
    </VBox>
    <HBox maxHeight="-Infinity" maxWidth="+Infinity" minHeight="-Infinity" minWidth="-Infinity" GridPane.columnIndex="1" GridPane.rowIndex="1">
      <children>
        <HBox fx:id="actionParent" alignment="CENTER">
          <HBox.margin>
            <Insets />
          </HBox.margin>
        </HBox>
        <Pane maxWidth="+Infinity" HBox.hgrow="ALWAYS">
               <children>
                  <TextField fx:id="RemoveNumTextFeild" prefHeight="25.0" prefWidth="96.0" promptText="num to remove" />
               </children></Pane>
        <Button fx:id="RemoveCancelButton" cancelButton="true" minWidth="80.0" mnemonicParsing="false" onAction="#HandelRemoveMain" text="Cancel" HBox.hgrow="NEVER">
          <HBox.margin>
            <Insets />
          </HBox.margin>
        </Button>
        <HBox fx:id="okParent" alignment="CENTER">
          <children>
            <Button fx:id="okButtonRemove" minWidth="80.0" mnemonicParsing="false" onAction="#HandelRemoveToBufferPopup" text="Ok" HBox.hgrow="NEVER">
              <HBox.margin>
                <Insets left="14.0" />
              </HBox.margin>
            </Button>
          </children>
        </HBox>
      </children>
    </HBox>
  </children>
  <columnConstraints>
    <ColumnConstraints hgrow="NEVER" maxWidth="55.0" minWidth="-Infinity" prefWidth="0.0" />
    <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" maxWidth="Infinity" minWidth="-Infinity" prefWidth="460.0" />
  </columnConstraints>
  <padding>
    <Insets bottom="14.0" left="14.0" right="14.0" top="14.0" />
  </padding>
  <rowConstraints>
    <RowConstraints maxHeight="+Infinity" minHeight="-Infinity" valignment="CENTER" vgrow="ALWAYS" />
    <RowConstraints maxHeight="-Infinity" minHeight="-Infinity" vgrow="NEVER" />
  </rowConstraints>
</GridPane>

控制器中使用的代码看起来像

    @FXML
    public Menu test;
    public TextField VarientAdminPanelText;
    public void PopulateModelBarAdd(){
        MenuItem item = new MenuItem(VarientAdminPanelText.getText());
        System.out.println(test.getItems());
        test.getItems().addAll(item);
    }

如果基于 FXML 甚至可以修改 GUI,这让我很受伤

将代码更改为

 public Menu test;
 @FXML //define FXML for VarientAdminPanelText
 public TextField VarientAdminPanelText;

    public void PopulateModelBarAdd(){
        MenuItem item = new MenuItem(VarientAdminPanelText.getText());
        System.out.println(test.getItems());
        test.getItems().addAll(item);
    }

而且我没有看到任何 fx:id 元素用于测试,如果您在 fxml 之外声明它,那么您需要将其初始化为

Menu test=new Menu()

我解决这个问题的方法是在初始化方法中初始化菜单项

public void initialize(URL location, ResourceBundle resources) {
    java.util.Iterator<String> itr = BufferManipulation.Menu.iterator();
    while( itr.hasNext()){
        AddVarMenu.getItems().addAll(new MenuItem(itr.next()));
        }
    java.util.Iterator<MenuItem> Item = AddVarMenu.getItems().iterator();
    while(Item.hasNext()){
        MenuItem MenuFeild = Item.next();
        MenuFeild.setOnAction(e -> AddVarMenu.setText(MenuFeild.getText()));
    }

    System.out.println("Inventory view is now loaded!");
}

问题似乎是场景加载后我们无法修改它,所以我们只是将菜单项放在一个对象中,我们可以在控制器之间传递,即在一个对象中修改该对象,然后将其加载到另一个对象中