ChoiceBox Overview

Woman working on a laptop at home
ONOKY - Eric Audras/Brand X Pictures/Getty Images

The

ChoiceBox
class is used to create a control which presents the user with a few choices to pick from a drop-down list. The user is only allowed to pick one of the options. When the drop-down list is not showing then the currently selected option is the only one visible. It is possible to set the
ChoiceBox

Import Statement

import javafx.scene.control.ChoiceBox;

Constructors

The

ChoiceBox

//Create an empty ChoiceBox
ChoiceBox choices = new ChoiceBox();
//Create a ChoiceBox using an observable list collection
ChoiceBox cboices = new ChoiceBox(FXCollections.observableArrayList("Apple", "Banana", "Orange", "Peach", "Pear", "Strawberry"));

Useful Methods

If you choose to create an empty

ChoiceBox
items can be added later using the
setItems

choices.setItems(FXCollections.observableArrayList("Apple", "Banana", "Orange", "Peach", "Pear", "Strawberry"));

And, if you want to find out what items are in a

ChoiceBox
you can use the
getItems

List options = choices.getItems();

To pick an option to be currently selected use the

setValue

choices.setValue("First");

To get the value of the option currently selected use the corresponding

getValue
method and assign it to a String

String option = choices.getValue().toString();

Event Handling

In order to listen to events for a

ChoiceBox
object, the
SelectionModel
is used. The
ChoiceBox
uses the
SingleSelectionModel
class which only permits one option to be chosen at a time. The
selectedIndexProperty
method allows us to add a
ChangeListener

final List options = choices.getItems();
choices.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
@Override public void changed(ObservableValue ov, Number oldSelected, Number newSelected) {

System.out.println("Old Selected Option: " + options.get(oldSelected.intValue()));
System.out.println("New Selected Option: " +options.get(newSelected.intValue()));

}
});

It's also possible to show or hide the list of options without the user having to click on the

ChoiceBox
object by using the
show
and
hide
methods. In the code below a Button object is used to call the show method of a
ChoiceBox
object when the
Button

//Use a stackpane for a simple layout of the controls
StackPane root = new StackPane();
//Create Button to show the options in the ChoiceBox
Button showOptionButton = new Button("Show Options");
root.getChildren().add(showOptionButton);
root.setAlignment(showOptionButton, Pos.TOP_CENTER);
//Create the ChoiceBox with a few options
final ChoiceBox choices = new ChoiceBox(FXCollections.observableArrayList("Apple", "Banana", "Orange", "Peach", "Pear", "Strawberry"));
root.getChildren().add(choices);
//Use the ActionEvent to call the ChoiceBox show method
showOptionButton.setOnAction(new EventHandler() {
@Override public void handle(ActionEvent e) {
choices.show();
}
});
//Set the Scene and put the Stage into motion..
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();

To find out about other JavaFX controls, have a look at JavaFX User Interface Controls.

Format
mla apa chicago
Your Citation
Leahy, Paul. "ChoiceBox Overview." ThoughtCo, Aug. 26, 2020, thoughtco.com/choicebox-overview-2033928. Leahy, Paul. (2020, August 26). ChoiceBox Overview. Retrieved from https://www.thoughtco.com/choicebox-overview-2033928 Leahy, Paul. "ChoiceBox Overview." ThoughtCo. https://www.thoughtco.com/choicebox-overview-2033928 (accessed January 27, 2021).