Example to set action listeners (behavior) to a ChoiceBox in JavaFX

JavaFXObject Oriented ProgrammingProgramming

A choice box holds a small set of multiple choices and, allows you to select only one of them. This will have two states showing and not showing. When showing, you can see the list of choices in a choice box and while not showing, it displays the current choice. By default, no option is selected in a choice box.

In JavaFX a choice box is represented by the class javafx.scene.control.ChoiceBox<T>. You can create a choice box by instantiating this class.

The selectionModel property of this class holds the selection model of the current choice box, you can get the value of it using the getSelectionModel() method (which is always SingleSelectionModel).

The SelectionModel class has two properties namely selectedIndex and selectedItem specifying the selected index and selected items respectively. To perform an action when an item is selected you can do so by adding action listener on to either of these properties using the addListener() method as −

getSelectionModel().selectedIndexProperty().addListener()
or,
getSelectionModel().selectedItemProperty().addListener()

Example

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class ChoiceBoxAction extends Application {
   public void start(Stage stage) {
      //Creating a choice box
      ChoiceBox<String> choiceBox = new ChoiceBox<String>();
      //Retrieving the observable list
      ObservableList<String> list = choiceBox.getItems();
      //Adding items to the list
      list.add("English");
      list.add("Hindi");
      list.add("Telugu");
      //Setting the position of the choice box
      choiceBox.setTranslateX(200);
      choiceBox.setTranslateY(15);
      //Setting the label
      Label label = new Label("Select Display Language:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      label.setTranslateX(20);
      label.setTranslateY(20);
      //Setting the text for message
      Text text = new Text(25.0, 130.0, "");
      text.setFont(Font.font("Britannic Bold", 33));
      text.setFill(Color.BROWN);
      String language[] = new String[]{
         "Welcome to Tutorilaspoint",
         "टुटोरियल्स पॉइन्ट को आप का स्वागत है",
         "ట్యూ టోరియల్స్ పాయింట్ కి స్వా గతిం"
      };
      //Adding action to the choice box
      choiceBox.getSelectionModel().selectedIndexProperty().addListener(
         (ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> {
            text.setText(language[new_val.intValue()]);
      });
      //Adding the choice box to the group
      Group root = new Group(choiceBox, label, text);
      //Setting the stage
      Scene scene = new Scene(root, 595, 170, Color.BEIGE);
      stage.setTitle("Choice Box Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

When selected English −

When selected Hindi −

When selected Telugu −

raja
Published on 18-May-2020 10:02:31
Advertisements