0

I have been trying to add items to a ListView. The items are created in a pop-up window, and i want to add them to the ListView in the main window.

I want to have some text from a TextField and it should add that text to the ListView in the main window.

I am very new to JavaFX and so I have been using "Scene Builder" and the FXMLDocumentController.java ONLY. No work has been done in the main.java.

So I need help on how I add the item from my popup window to my ListView in the main window in a method in the controller document.

(Both FXML have same controller)

public class FXMLDocumentController implements Initializable {

ObservableList<String> list = FXCollections.observableArrayList("mark", "tom");

@FXML
private ListView<String> itemList = new ListView<String>(list);

@FXML
private Button tilføjGin = new Button();

@FXML
private TextArea GinNoter = new TextArea();

@FXML
private TextField GinNavn = new TextField();

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    itemList.setItems(list);
}    


public void gemGin(ActionEvent event) {
    Gin g = new Gin(GinNavn.getText(), GinNoter.getText());
//       list.add(g.getName());                                <--
//       itemList.setItems(list);                             <--
      ginAdd(g);                                             <--These dont work for me.

    Stage stage2 = (Stage) tilføjGin.getScene().getWindow();
    stage2.close();
} 

public void ginAdd(Gin gin) {
    list.add(gin.getName());                        <--
    itemList.setItems(list);                        <-- None of these seems to work.
//  itemList.getItems().add(gin.getName());         <--
}

@FXML
public void createNewGin(ActionEvent event) throws IOException {

 Stage stage = new Stage(); 
 Parent root;

 root = FXMLLoader.load(getClass().getResource("FXML-PopUp.fxml"));
                                                                            <-- My pop-up window
 //create a new scene with root and set the stage
 stage.setScene(new Scene(root));                    
 stage.setTitle("My modal window");
 stage.initModality(Modality.APPLICATION_MODAL);
 stage.showAndWait();

}

If you want to see my FXML: pop-up window fxml:

<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="800.0" 
xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="farginapp2.FXMLDocumentController">
   <children>
      <Label layoutX="25.0" layoutY="14.0" text="Ny Gin">
         <font>
            <Font name="System Bold Italic" size="26.0" />
         </font>
      </Label>
      <Label layoutX="25.0" layoutY="72.0" text="Navn" />
      <Label layoutX="25.0" layoutY="144.0" text="Årgang" />
      <Label layoutX="25.0" layoutY="223.0" text="Oprindelse" />
      <Label layoutX="25.0" layoutY="300.0" text="Alkohol %" />
      <TextField fx:id="GinNavn" layoutX="25.0" layoutY="97.0"             
promptText="&quot;Gin Ginsen&quot;" />
      <TextField fx:id="GinAlder" layoutX="25.0" layoutY="170.0" 
promptText="&quot;10.000 BC&quot;" />
      <TextField fx:id="GinLand" layoutX="25.0" layoutY="247.0"     
promptText="&quot;Vatikanet&quot;" />
      <TextField fx:id="GinAlkohol" layoutX="25.0" layoutY="325.0" 
prefHeight="121.0" prefWidth="172.0" promptText="&quot;98%&quot;" />
      <Label layoutX="497.0" layoutY="52.0" text="Noter ">
         <font>
            <Font name="System Bold" size="16.0" />
         </font>
      </Label>
      <Button fx:id="tilføjGin" layoutX="345.0" layoutY="529.0" 
mnemonicParsing="false" onAction="#gemGin" prefHeight="49.0" 
prefWidth="110.0"     
    text="Tilføj Gin">
         <font>
            <Font name="System Bold" size="19.0" />
         </font>
      </Button>
      <TextArea fx:id="GinNoter" layoutX="483.0" layoutY="272.0"     
prefHeight="200.0" prefWidth="200.0" />
   </children>
    </AnchorPane>

Main window FXML:

<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="farginapp2.FXMLDocumentController">
   <children>
      <MenuBar>
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="CloseProgram" mnemonicParsing="false" onAction="#FileMenuAction1" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
                  <MenuItem fx:id="newGin" mnemonicParsing="false" onAction="#createNewGin" text="New Gin" />
              <MenuItem mnemonicParsing="false" text="Delete Gin" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <SplitPane dividerPositions="0.18485523385300667, 0.7527839643652561" prefHeight="607.0" prefWidth="900.0">
         <items>
            <AnchorPane prefHeight="542.0" prefWidth="149.0">
               <children>
                  <ListView fx:id="itemList" layoutY="73.0" onMouseClicked="#listMouseAction" prefHeight="469.0" prefWidth="163.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="73.0" />
               </children></AnchorPane>
            <ScrollPane prefHeight="542.0" prefWidth="500.0">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="542.0" prefWidth="504.0">
                     <children>
                        <Button fx:id="BUTT_1" layoutX="226.0" layoutY="221.0" mnemonicParsing="false" onAction="#Butt1Action" text="CLICK MIG!" />
                        <Label fx:id="LabelMid" alignment="CENTER" layoutX="117.0" layoutY="47.0" prefHeight="140.0" prefWidth="271.0" text="Label" textAlignment="CENTER">
                           <font>
                              <Font name="System Bold" size="50.0" />
                           </font></Label>
                     </children></AnchorPane>
              </content>
            </ScrollPane>
            <AnchorPane prefHeight="542.0" prefWidth="357.0">
               <children>
                  <TextField fx:id="noter" layoutX="10.0" layoutY="50.0" prefHeight="480.0" prefWidth="200.0">
                     <padding>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </padding>
                  </TextField>
                  <Label layoutX="14.0" layoutY="3.0" prefHeight="47.0" prefWidth="82.0" text="Noter">
                     <padding>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </padding>
                     <font>
                        <Font name="System Bold" size="18.0" />
                     </font>
                  </Label>
               </children></AnchorPane>
         </items>
      </SplitPane>
      <HBox prefHeight="94.0" prefWidth="900.0">
         <children>
            <Label prefHeight="17.0" prefWidth="41.0" text="Label" />
            <Label alignment="CENTER_RIGHT" text="Label" />
         </children>
      </HBox>
   </children>
</VBox>
  • 1. Don't use the same controller class for different FXML files. Use a different controller class for each of the FXML. 2. Never initialize fields annotated `@FXML`. The annotation means the `FXMLLoader` will initialize them. If you are getting null pointer exceptions because these are null, something else is wrong which needs to be fixed. – James_D Dec 23 '17 at 14:26
  • So separate the controller into two classes, one for each FXML. When you load the FXML for the popup window, [pass the list to its controller](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml), so the controller for the popup can add items to that list (the new item will then appear in the list view automatically). – James_D Dec 23 '17 at 14:30

0 Answers0