Fill in the Choicebox defined in FXML

I am learning javaFX and my problem is that I have a simple window with a few selection buttons and a button. This window is defined through FXML, which is also associated with the controller class. I would like to know how to populate this selection box with data in the controller class, because using the @FXML link to this checkbox select 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 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 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();
    }
}
}

Thanks for the help and time.

+4
source share
1 answer

How to fix it

Delete the line countChoiceBox = new ChoiceBox();and everything will work fine if you don't have other errors elsewhere in your application.

countChoiceBox, node, FXMLLoader .

FXML onChangeCountClick :

  • supermarket.ManageWindowCC.
  • FXML.
  • ChoiceBox.
  • ChoiceBox, FXML, countChoiceBox.
  • , root, .

, FXML, countChoiceBox ChoiceBox, FXMLLoader

.,.

() :

countChoiceBox = new ChoiceBox();

, , β†’ new, @FXML.


. ComboBox FXML ( ComboBox FXML, ).

+2

Source: https://habr.com/ru/post/1536490/


All Articles