JavaFX: Setting up a choice box

This entry is part 32 of 55 in the series JavaFX

We commonly called a choice box a drop-down list but somehow in Java, it is called a choice box.

Just like radio button or check box, it is relatively easy to set up a choice box.

The ChoiceBox class is generic, so specify a type for the list that will be associated with the choice box.  For example, we are creating a drop-down list of strings in the statement below:

ChoiceBox<String> choice = new ChoiceBox<String>();

To add an item to the list, you can do that by calling the method to access the list of items and then calling the add method to add an item.


By default, a choice box has no initial selection when it’s first displayed. To set an initial value, we can call the setValue method.

Consider the example below:

package javafxapplication25;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.Priority;

public class JavaFXApplication25 extends Application {

    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        ChoiceBox<String> choice = new ChoiceBox<String>();


        HBox hboxPane = new HBox(10); // spacing = 10       
        hboxPane.setPadding(new Insets(10, 10, 10, 10));

        Label lb = new Label("Selection:");

        hboxPane.setHgrow(choice, Priority.ALWAYS);

        hboxPane.getChildren().addAll(lb, choice);

        Scene scene = new Scene(hboxPane, 250, 200);

    public static void main(String[] args) {


Series Navigation<< JavaFX: Setting up radio button group
JavaFX: Putting Objects Into Choice Box >>