ComboBox items through script?

<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 couldn’t figure out where I can add elements to my ComboBox in SceneBuilder. Is it possible to add elements through SceneBuilder, or do I need to do this manually?

+6
source share
2 answers

You cannot add items to combobox through SceneBuilder. Either you can add through the FXML file, like you, or through the controller, as shown 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"); } 
+8
source

In response to saikosen's comment : if the Controller is not implementing an Initializable implementation, 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"); } 
+1
source

All Articles