2016-06-14 3 views
-1

Je travaille sur un projet et je veux avoir un menu dans lequel les utilisateurs peuvent ajouter et supprimer des éléments. J'ai vu la documentation java qui dit utiliser getItems sur votre objet de menu mais cela me donne une erreur de point nul à l'exécution "Causée par: java.lang.NullPointerException à app.Main_Controller.PopulateModelBarAdd (Main_Controller.java:257)"JavaFX FXML modifier la liste

<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> 

le fichier FXML où le menu est:

<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> 

le code utilisé dans le contrôleur ressemble

@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); 
    } 

Thi s me fait wounder s'il est même possible de modifier l'interface graphique quand il est basé sur des FXML

Répondre

0

la façon dont je résolu ce problème a été pour initialiser les éléments de menu dans la méthode initialize

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!"); 
} 

Le problème semble être que nous ne pouvons pas modifier la scène une fois qu'elle est chargée, donc nous plaçons simplement les éléments de menu dans un objet que nous pouvons passer du contrôleur au contrôleur, par exemple en le modifiant dans un autre

-1

Modifier le code de

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); 
    } 

et je ne vois pas fx: élément id pour le test si vous déclarez à l'extérieur FXML alors vous devez l'initialiser comme

Menu test=new Menu() 
+0

Pourquoi est-ce que le fait de changer l'emplacement du tag le change? – ajjohnson

+0

le menu est initialisé dans un fichier FXML dif – ajjohnson

+0

Je ne vous ai pas eu "\t Pourquoi est-ce que le fait de changer où la balise va le changer?" –