Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate Choicebox defined in FXML

Tags:

javafx

fxml

I'm learning javaFX and my problem is that I have simple window with some choicebox and button. This window is defined via FXML which is also associated with controller class. I would like to know, how to populate this choicebox with data in the controller class, because using @FXML reference to this choicebox throwsNullpointerEception

EDIT - added source code FXML code

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="240.0"
        prefWidth="320.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
        fx:controller="supermarket.ManageWindowCC">
<children>
    <ChoiceBox fx:id="countChoiceBox" layoutX="44.0" layoutY="71.0" prefHeight="25.0" prefWidth="191.0"/>
    <Label layoutX="44.0" layoutY="54.0" text="To change item's count, choose one"/>
    <TextField layoutX="140.0" layoutY="129.0" prefHeight="25.0" prefWidth="24.0"/>
    <Label layoutX="123.0" layoutY="112.0" text="New count"/>
    <Button layoutX="126.1875" layoutY="171.5" mnemonicParsing="false" text="Submit"/>
</children>

Java controller code:

public class ManageWindowCC {
@FXML
private ChoiceBox countChoiceBox;

public void onChangeCountClick(ActionEvent actionEvent) {

    try {
        Parent root = FXMLLoader.load(getClass().getResource("ChangeCount.fxml"));
        Stage newStage = new Stage();
        newStage.setTitle("Change item's count");
        newStage.setScene(new Scene(root, 320, 240));
        newStage.show();
        countChoiceBox = new ChoiceBox();
        countChoiceBox.setItems(FXCollections.observableArrayList("One","Two","Three"));

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Thank you fro your help and time

like image 320
user2151486 Avatar asked Oct 01 '22 01:10

user2151486


1 Answers

How to fix it

Remove the line countChoiceBox = new ChoiceBox(); and everything will work fine, assuming you have no other bugs elsewhere in your application.

The program will use the reference to the countChoiceBox which is part of the node hierarchy created by the FXMLLoader and set in your scene.

What is happening

Loading a new FXML in onChangeCountClick will:

  1. Create a new supermarket.ManageWindowCC controller.
  2. Create a hierarchy of Nodes based upon the FXML definition.
  3. One of the nodes in the hierarchy will be a ChoiceBox.
  4. The ChoiceBox which the FXML loader automatically creates for you will be assigned to the countChoiceBox member.
  5. You then take the hierarchy of nodes assigned to root and add it to your new Scene on your new Stage.

So, after you load the FXML, countChoiceBox is initialized to an empty ChoiceBox instantiated by your FXMLLoader

This is all fine so far . . .

What you then do is (incorrectly) write:

countChoiceBox = new ChoiceBox();

The rule of thumb you violate is => never use new to assign a value to a members tagged @FXML.


Also see the somewhat related example for populating a ComboBox using FXML (though that sample uses a ComboBox and populates its data directly in FXML, so it is not directly applicable to your situation).

like image 76
jewelsea Avatar answered Oct 06 '22 01:10

jewelsea