Updated July 3, 2023
Introduction to JavaFX ChoiceBox
ChoiceBox of JavaFX is available in the scene.control package. ChoiceBox has a set of values and allows the user to select only a single value from a list of choice values. ChoiceBox always shows the selected value to the end user at the top of the box. ChoiceBox, by default, does not select any value; if we want, we can set the value explicitly by the JavaFX ChoiceBox code. Countries, States, Districts, Villages, etc. Choice values used ChoiceBox.
Constructors:
- ChoiceBox(): It creates no parameterized ChoiceBox instance.
- ChoiceBox(ObservableList listItems): It creates ObservableList parameterized ChoiceBox for accepting a list of values.
Frequently Used Methods:
- setItems(ObservableList value): It adds all the list items to the ChoiceBox instance.
- setValue(T value): Sets the single choice value simultaneously.
- getItems(): It gives the items of the ChoiceBox.
- getValue(): It reads the ChoiceBox value means to get the value.
- show(): Opens the list of all choices from ChoiceBox.
- hide(): It closes the list of choices from ChoiceBox.
How does ChoiceBox Work in JavaFX?
Accessing JavaFX features user-defined class must extend the Application class.
1. In JavaFX, creating is the first step. ChoiceBox can instantiate by using a new keyword.
ChoiceBox choiceBoxRef=new ChoiceBox ();
2. Adding elements or items to the choiceBoxRef is the second step. Items can be added in 2 ways.
- By using add() method
choiceBoxRef.getItems().add("item-name");
- By using ObservableList class
ObservableList<Type> names = FXCollections.observableArrayList(list of items);
ObservavleList<Type>: Holds the list of the items with Type.
FXCollections.observableArrayList(): Takes all possible Typed list of items.
3. The third step is to create Tile Pane or any other display class to add the items.
TilePane tPane=new TilePane (); //Gives horizontal box
4. Creating a scene means a screen to display output is the fourth step.
Scene screen = new Scene(tPane, length, width);
5. The fifth step is Adding a Scene reference screen to the Stage object reference. Adding output screen to Stage. We will get this stage object reference from the start predefined JavaFX method.
stage.setScene(screen);
6. At last, display output screen by showing stage reference with the show ()
stage.show();
Examples of JavaFX ChoiceBox
Given below are the examples:
Example #1
Choice Box with Country Names.
Code: CountryNamesChoiceBox.java
package com.choicebox;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class CountryNamesChoiceBox extends Application {
@Override
public void start(Stage stage) {
/* set a title to screen */
stage.setTitle("Country Names for Choice Box");
Label lableCountry = new Label("Choice Box Demo for Country Names");
/* Creating a choice box with default constructor */
ChoiceBox<Object> choiceBox = new ChoiceBox<Object>();
choiceBox.getItems().add("India");
choiceBox.getItems().add("Brazil");
choiceBox.getItems().add("Bermuda");
choiceBox.getItems().add("Sweeden");
choiceBox.getItems().add("Newzealand");
choiceBox.getItems().add("Pakistan");
choiceBox.getItems().add("Australia");
choiceBox.getItems().add("Afghanistan");
choiceBox.getItems().add("South Africa");
/* Creating a tile pane for adding choice box */
TilePane tilePane = new TilePane();
tilePane.getChildren().add(lableCountry);
tilePane.getChildren().add(choiceBox);
/* Creating a scene for adding tile pane */
Scene scene = new Scene(tilePane, 300, 300);
/* adding scene to stage */
stage.setScene(scene);
/* display scene for showing output */
stage.show();
}
public static void main(String args[]) {
/* launch method calls internally start() method */
launch(args);
}
}
Output:
Explanation:
- As you can see, we got a drop-down choice box with country values. By default, the choice box is non-editable.
Example #2
Disabling Choice Box.
Code: DisablingChoiceBox.java
package com.choicebox;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class DisablingChoiceBox extends Application {
@Override
public void start(Stage stage) {
/* set a title to screen */
stage.setTitle("Country Names Choice Box");
Label lableCountry = new Label("Disabling Country Names Choice Box");
/* Creating a choice box with default constructor */
ChoiceBox<Object> choiceBox = new ChoiceBox<Object>();
choiceBox.getItems().add("India");
choiceBox.getItems().add("Brazil");
choiceBox.getItems().add("Bermuda");
choiceBox.getItems().add("Sweeden");
choiceBox.getItems().add("Newzealand");
choiceBox.getItems().add("Pakistan");
choiceBox.getItems().add("Australia");
choiceBox.getItems().add("Afghanistan");
choiceBox.getItems().add("South Africa");
/*setting choice box property to disable*/
choiceBox.setDisable(true);
/* Creating a tile pane for adding choice box */
TilePane tilePane = new TilePane();
tilePane.getChildren().add(lableCountry);
tilePane.getChildren().add(choiceBox);
/* Creating a scene for adding tile pane */
Scene scene = new Scene(tilePane, 400, 200);
/* adding scene to stage */
stage.setScene(scene);
/* display scene for showing output */
stage.show();
}
public static void main(String args[]) {
/* launch method calls internally start() method */
launch(args);
}
}
Output:
Explanation:
- As you can see, the choice box is disabled by using setDisable(true)
Example #3
Display selected Choice.
Code: ReadSelectValueChoiceBox.java
package com.choicebox;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class ReadSelectValueChoiceBox extends Application {
@Override
public void start(Stage stage) {
/* set a title to screen */
stage.setTitle("Country Names");
Label lableCountry = new Label("List of Countries");
// string array
String countryValues[] = { "India", "Newzealand", "Pakistan", "Australia","Afghanistan","South Africa" };
/* Creating a combobox with default constructor */
ChoiceBox<Object> comboBox = new ChoiceBox<Object>(FXCollections.observableArrayList(countryValues));
Label selectLabelText = new Label("nothing selected");
//adding listener for getting selected values from Choice Box
comboBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
// if the item of the list is changed
public void changed(ObservableValue ov, Number value, Number new_value)
{
// set the text for the label to the selected item
selectLabelText.setText(countryValues[new_value.intValue()] + " country is selected");
}
});
/* Creating a tile pane for adding combobox */
TilePane tilePane = new TilePane();
tilePane.getChildren().add(lableCountry);
tilePane.getChildren().add(comboBox);
tilePane.getChildren().add(selectLabelText);
/* Creating a scene for adding tile pane */
Scene scene = new Scene(tilePane, 250, 250);
/* adding scene to stage */
stage.setScene(scene);
/* display scene for showing output */
stage.show();
}
public static void main(String args[]) {
/* launch method calls internally start() method */
launch(args);
}
}
Output:
Explanation:
- As you can see from the output, when we select any choice then, that choice is displayed on the label.
Recommended Articles
We hope that this EDUCBA information on “JavaFX ChoiceBox” was beneficial to you. You can view EDUCBA’s recommended articles for more information.