JavaFX – ChoiceBox


JavaFX – ChoiceBox



”;


A ChoiceBox is a drop-down list of predefined choices that allows users to select one of them at a time. It always displays the currently selected option on the top of the box, and when the user clicks on the ChoiceBox, it shows a drop-down list of all other available choices.

In JavaFX, a choicebox is represented by a class named ChoiceBox. This class belongs to the package javafx.scene.control. By instantiating this class, we can create an ChoiceBox node in JavaFX.

Two constructors are available for this class, and they are as follows −

  • ChoiceBox() − It is used for creating an accordion without TitledPane.

  • ChoiceBox(ObservableList items) − It will create an accordion with the specified TitledPane.

Creating a Choice Box in JavaFX

We need to follow the steps given below to create a ChoiceBox in JavaFX.

Step 1: Instantiate the ChoiceBox class

In JavaFX, the choice boxes are created by instantiating the class named ChoiceBox which belongs to a package javafx.scene.control. Instantiate this class inside the start() method as shown below −


//Instantiating the ChoiceBox class
ChoiceBox<String> box = new ChoiceBox<String>();

Step 2: Add Items to the ChoiceBox

We use the ObservableList to add the items to a ChoiceBox in JavaFX. It holds all the items with specified type such as String.


//Retrieving the observable list
ObservableList<String> oslist = box.getItems();
//Adding items to the list
oslist.addAll("Windows7", "Windows8", "Windows10", "Windows11", "MAC OS"); 

Note − Sometimes, we may require to specify additional description for ChoiceBox, in such cases, we can use the Label or TextField class of JavaFX.

Step 3: Launch the Application

Once the ChoiceBox is created and its items are added, follow the given steps below to launch the application properly −

  • Firstly, instantiate the class named Scene by passing the ChoiceBox object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor.

  • Then, set the title to the stage using the setTitle() method of the Stage class.

  • Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  • Display the contents of the scene using the method named show().

  • Lastly, the application is launched with the help of the launch() method.

Example

The following JavaFX program demonstrates how to use a ChoiceBox in a JavaFX application. Save this code in a file with the name JavafxChoiceBox.java.


import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class JavafxChoiceBox extends Application {
   public void start(Stage stage) {
      //Instantiating the ChoiceBox class
      ChoiceBox<String> box = new ChoiceBox<String>();
      //Retrieving the observable list
      ObservableList<String> oslist = box.getItems();
      //Adding items to the list
      oslist.addAll("Windows7", "Windows8", "Windows10", "Windows11", "MAC OS");
      //Setting the position of the choice box
      box.setTranslateX(200);
      box.setTranslateY(15);
      //Setting the label
      Label setlabel = new Label("Select your Operating System:");
      setlabel.setTranslateX(20);
      setlabel.setTranslateY(20);
      //Adding the choice box to the group
      Group newgrp = new Group(box, setlabel);
      //Setting the stage
      Scene scene = new Scene(newgrp, 500, 200);
      stage.setTitle("Choice Box in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxChoiceBox.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxChoiceBox

Output

When we execute the above code, it will generate the following output.


ChoiceBox Output

Creating a Choice Box using its Parameterized Constructor

Previously, we have used the empty constructor of the ChoiceBox class to create a choice box. However, there is also another way available for the same. We can use the parameterized constructor by passing the list items using observableArrayList as an argument.

Example

Following is the JavaFX program which will create a Choice Box using Parameterized Constructor of ChoiceBox class. Save this code in a file with the name JavafxChoiceBox.java.


import javafx.application.Application;
import javafx.collections.*;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class JavafxChoiceBox extends Application {
   public void start(Stage stage) {
      //Creating a ChoiceBox
      ChoiceBox<String> box = new ChoiceBox<String> (
         FXCollections.observableArrayList(
	     "Windows7", "Windows8", "Windows10", "Windows11", "MAC OS"));
      //Setting the position of the choice box
      box.setTranslateX(200);
      box.setTranslateY(15);
      //Setting the label
      Label setlabel = new Label("Select your Operating System:");
      setlabel.setTranslateX(20);
      setlabel.setTranslateY(20);
      //Adding the choice box to the group
      Group newgrp = new Group(box, setlabel);
      //Setting the stage
      Scene scene = new Scene(newgrp, 500, 200);
      stage.setTitle("Choice Box in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}  

Compile and execute the saved Java file from the command prompt using the following commands.


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxChoiceBox.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxChoiceBox

Output

On executing, the above program generates a JavaFX window displaying the below output.


ChoiceBox Output2

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *