ComboBox Overview

Man working at computer
Lina Aidukaite/Moment/Getty Images

The ComboBox class creates a control that allows the user to select an option from a drop-down list of options. The drop-down list appears when the user clicks on the ComboBox control. When the number of options exceeds the size of the drop-down window, the user can scroll down to further options. This differs from the ChoiceBox which is primarily used when the number of choices is a relatively small set.

Import Statement

javafx.scene.control.ComboBox

Constructors

The ComboBox class has two constructors depending on whether you want to create an empty ComboBox object or one populated with items.

To Create an Empty ComboBox

ComboBox fruit = new ComboBox();

To create a ComboBox object and populate it with String items from an ObservableList

ObservableList fruits = FXCollections.observableArrayList( 
"Apple", "Banana", "Pear", "Strawberry", "Peach", "Orange", "Plum");
ComboBox fruit = new ComboBox(fruits);

Useful Methods

If you create an empty ComboBox object you can use the setItems method. Passing an ObservableList of objects will set the items in the Combobox.

ObservableList fruits = FXCollections.observableArrayList( 
"Apple", "Banana", "Pear", "Strawberry", "Peach", "Orange", "Plum");
fruit.setItems(fruits);

If you want to add items to the ComboBox list later on you can use the addAll method of the getItems method. This will append the items to the end of the options list:

fruit.getItems().addAll("Melon", "Cherry", "Blackberry");

To add an option to a particular place in the ComboBox option list use the add method of the getItems method. This method takes an index value and the value you wish to add:

fruit.getItems().add(1, "Lemon");

Note: The index values of the ComboBox start at 0. For example, the above value of "Lemon" above will be inserted into the ComboBox option list at position 2 as the index passed is 1.

To pre-select an option in the ComboBox options list, use the setValue method:

fruit.setValue("Cherry");

If the value passed to the setValue method is not on the list, then the value will still be selected. However, it does not mean this value has been added to the list. If the user subsequently picks another value then the initial value will no longer be in the list to be selected.

To get the value of the currently selected item in the ComboBox, use the getItems method:

String selected = fruit.getValue().toString();

Usage Tips

The number of options normally presented by the ComboBox dropdown list is ten (unless there are less than ten items in which case it defaults to the number of items). This number can be changed by using the setVisibleRowCount method:

fruit.setVisibleRowCount(25);

Again, if the number of items in the list is less than the value set in the setVisibleRowCount method the ComboBox will default to displaying the number of items in the ComboBox dropdown.

Handling Events

To track the selection of items on a ComboBox object you can use the addListener method of the selectedItemProperty method of the SelectionModel to create a ChangeListener It will pick up the change events for the ComboBox:

final Label selectionLabel = new Label();
fruit.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener() {
public void changed(ObservableValue ov,
String old_val, String new_val) {
selectionLabel.setText(new_val);
}
});
Format
mla apa chicago
Your Citation
Leahy, Paul. "ComboBox Overview." ThoughtCo, Aug. 26, 2020, thoughtco.com/combobox-overview-2033930. Leahy, Paul. (2020, August 26). ComboBox Overview. Retrieved from https://www.thoughtco.com/combobox-overview-2033930 Leahy, Paul. "ComboBox Overview." ThoughtCo. https://www.thoughtco.com/combobox-overview-2033930 (accessed March 19, 2024).