<ComboBox fx:id="schaltung" layoutX="347.0" layoutY="50.0" prefHeight="63.0" prefWidth="213.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:id="reihe" fx:value="Reihenschaltung" />
<String fx:id="parallel" fx:value="Parallelschaltung" />
</FXCollections>
</items>
</ComboBox>
I added this to my FXML file because I couldnt figure out where I could add Items to my ComboBox in the SceneBuilder. Is it possible to add items via the SceneBuilder, or do I have to do it manually?
ComboBox is a part of the JavaFX library. JavaFX ComboBox is an implementation of simple ComboBox which shows a list of items out of which user can select at most one item, it inherits the class ComboBoxBase.
The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only.
You can't add items to combobox through SceneBuilder. Either you can add through FXML file as you did or through controller as given below.
@Override
public void initialize(URL location, ResourceBundle resources) {
comboBox.getItems().removeAll(comboBox.getItems());
comboBox.getItems().addAll("Option A", "Option B", "Option C");
comboBox.getSelectionModel().select("Option B");
}
In response to saikosen comment: If Controller does not implement Initializable
you can use :
@FXML
public void initialize() {
comboBox.getItems().removeAll(comboBox.getItems());
comboBox.getItems().addAll("Option A", "Option B", "Option C");
comboBox.getSelectionModel().select("Option B");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With