How to Get Selected Item From a ChoiceBox in JavaFX

Sheeraz Gul Feb 02, 2024
How to Get Selected Item From a ChoiceBox in JavaFX

ChoiceBox is a part of the JavaFX library from which we can get a selected choice. This tutorial demonstrates how to get a selected item from a ChoiceBox in JavaFX.

Get Selected Item From a ChoiceBox in JavaFX

ChoiceBox contains a set of items from which a user can select a choice that will be the currently selected item. The default choice in a choice box is a null entry.

The following methods are utilized to work with the ChoiceBox.

Method Description
hide() This method will close the list of choices.
setItems(ObservableList value) This sets the value of the property items.
setValue(T value) This will set the value of the property value.
getItems() This will get the value of the property items.
getValue() This gets the value of the property value.
show() This will open the list of choices.

We use the following method to get the selected item from a choice box.

ChoiceBox.getSelectionModel().selectedIndexProperty()

Let’s get a selected item from a ChoiceBox using JavaFX.

package delftstack;

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Choice_Box extends Application {
  public void start(Stage Choice_Box_Stage) {
    // title for the stage
    Choice_Box_Stage.setTitle("ChoiceBox");

    // button to show
    Button Show_Button = new Button("Show Choice");

    // tile pane
    TilePane Title_Pane = new TilePane();

    // labels
    Label Label1 = new Label("This is a choice box, Please select your choice");
    Label Label2 = new Label("No Choice selected");

    // Choices array
    String Choice_Array[] = {
        "Delftstack 1", "Delftstack 2", "Delftstack 3", "Delftstack 4", "Delftstack 5"};

    // choiceBox
    ChoiceBox DemoChoiceBox = new ChoiceBox(FXCollections.observableArrayList(Choice_Array));

    // adding a listener
    DemoChoiceBox.getSelectionModel().selectedIndexProperty().addListener(
        new ChangeListener<Number>() {
          // if items of the list are changed
          public void changed(ObservableValue ov, Number value, Number new_value) {
            // text for the label to the selected item
            Label2.setText(Choice_Array[new_value.intValue()] + " is Selected");
          }
        });

    // ChoiceBox
    Title_Pane.getChildren().add(Label1);
    Title_Pane.getChildren().add(DemoChoiceBox);
    Title_Pane.getChildren().add(Label2);

    Scene sc = new Scene(Title_Pane, 400, 200);

    // Setting the scene
    Choice_Box_Stage.setScene(sc);

    Choice_Box_Stage.show();
  }

  public static void main(String args[]) {
    // launching the application
    launch(args);
  }
}

The code above demonstrates getting a select item from a ChoiceBox with JavaFX. See output:

Get Selected Item From a ChoiceBox

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java JavaFX