Science, Tech, Math › Computer Science ChoiceBox Overview Share Flipboard Email Print ONOKY - Eric Audras/Brand X Pictures/Getty Images Computer Science Java Programming PHP Programming Perl Python Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Paul Leahy Paul Leahy Computer Science Expert M.A., Advanced Information Systems, University of Glasgow Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer. Learn about our Editorial Process Updated on July 03, 2019 TheChoiceBoxclass 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 TheChoiceBox //Create an empty ChoiceBoxChoiceBox choices = new ChoiceBox();//Create a ChoiceBox using an observable list collectionChoiceBox cboices = new ChoiceBox(FXCollections.observableArrayList("Apple", "Banana", "Orange", "Peach", "Pear", "Strawberry")); Useful Methods If you choose to create an emptyChoiceBox 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 aChoiceBox you can use the getItems List options = choices.getItems(); To pick an option to be currently selected use thesetValue 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 aChoiceBox 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 theChoiceBox 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 controlsStackPane root = new StackPane();//Create Button to show the options in the ChoiceBoxButton showOptionButton = new Button("Show Options");root.getChildren().add(showOptionButton);root.setAlignment(showOptionButton, Pos.TOP_CENTER);//Create the ChoiceBox with a few optionsfinal ChoiceBox choices = new ChoiceBox(FXCollections.observableArrayList("Apple", "Banana", "Orange", "Peach", "Pear", "Strawberry"));root.getChildren().add(choices);//Use the ActionEvent to call the ChoiceBox show methodshowOptionButton.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. Cite this Article 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 March 21, 2023). copy citation