Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox Items via Scene Builder?

Tags:

java

javafx

 <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?

like image 304
yemerra Avatar asked Feb 07 '16 22:02

yemerra


People also ask

What is a combo box in JavaFX?

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.

What is the difference between ComboBox and ChoiceBox JavaFX?

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.


2 Answers

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");
}
like image 196
Venkat Prasanna Avatar answered Oct 21 '22 14:10

Venkat Prasanna


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");
}
like image 27
c0der Avatar answered Oct 21 '22 12:10

c0der