JavaFX | ChoiceDialog

19 Apr, 2021
ChoiceDialog is part of JavaFX framework. ChoiceDialog is a dialog that gives the user a set of options from which the user can select at most one option. The Choice class is the derived class of the base class Dialog.Constructor for the class are:  
ChoiceDialog(): creates an empty choice dialog with no items.ChoiceDialog(T d, Collection choices) : creates a choice dialog with a set of items form the collection provided and a selected itemChoiceDialog(T d, T... choices): creates a choice dialog with a default selected item and an array of available choices for the user .
ChoiceDialog(): creates an empty choice dialog with no items.
ChoiceDialog(T d, Collection choices) : creates a choice dialog with a set of items form the collection provided and a selected item
ChoiceDialog(T d, T... choices): creates a choice dialog with a default selected item and an array of available choices for the user .
Commonly used methods: 
 
Below programs illustrate the ChoiceDialog class:  
Program to create a choice dialog and display it: This program creates a choice dialog d and an array of strings days which contains the names of the days of the week. This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function. On clicking the button the choice dialog appears with a selected item. The user is allowed to select an item from the choices offered.  
Program to create a choice dialog and display it: This program creates a choice dialog d and an array of strings days which contains the names of the days of the week. This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function. On clicking the button the choice dialog appears with a selected item. The user is allowed to select an item from the choices offered.  
Java
// Java Program to create a choice dialog and display itimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;public class choice_1 extends Application {     // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating choice dialog");         // create a button        Button b = new Button("click");         // create a tile pane        TilePane r = new TilePane();         // items for the dialog        String days[] = { "Monday", "Tuesday", "Wednesday",                                       "Thursday", "Friday" };         // create a choice dialog        ChoiceDialog d = new ChoiceDialog(days[1], days);         // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>()        {            public void handle(ActionEvent e)            {                 // show the dialog                d.show();            }        };         // when button is pressed        b.setOnAction(event);         // add button        r.getChildren().add(b);         // create a scene        Scene sc = new Scene(r, 200, 200);         // set the scene        s.setScene(sc);         s.show();    }     public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:  
Output:  
Program to create a choice dialog and add header and content text and also add a label to display the selected choice: This program creates a choice dialog d and an array of strings days which contains the names of the days of the week. This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function. On clicking the button the choice dialog appears with a selected item. The user is allowed to select an item from the choices offered. we also create a label l to display the currently selected item. we will also set the header text and content text using the setHeaderText() and setContentText() functions . we will get the selected item using the getSelectedItem() function. 
Program to create a choice dialog and add header and content text and also add a label to display the selected choice: This program creates a choice dialog d and an array of strings days which contains the names of the days of the week. This program creates a Button indicated by the name b. The button will be created inside a scene, which in turn will be hosted inside a stage. We would create a label to show if the button is pressed or not. The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the button and label inside the scene. Finally, the show() method is called to display the final results.we would create an event handler to handle the button events. The event handler would be added to the button using setOnAction() function. On clicking the button the choice dialog appears with a selected item. The user is allowed to select an item from the choices offered. we also create a label l to display the currently selected item. we will also set the header text and content text using the setHeaderText() and setContentText() functions . we will get the selected item using the getSelectedItem() function. 
Java
// Java Program to create a choice dialog and add header// and content text and also add a label to// display the selected choiceimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.control.Alert.AlertType;public class choice_2 extends Application {     // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating choice dialog");         // create a button        Button b = new Button("click");         // items for the dialog        String days[] = { "Monday", "Tuesday", "Wednesday",                                       "Thursday", "Friday" };         // create a label        Label l = new Label(days[1] + " selected");         // create a tile pane        TilePane r = new TilePane();         // create a choice dialog        ChoiceDialog d = new ChoiceDialog(days[1], days);         // action event        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                // setheader text                d.setHeaderText("day of the week");                 // set content text                d.setContentText("please select the day of the week");                 // show the dialog                d.showAndWait();                 // get the selected item                l.setText(d.getSelectedItem() + " selected");            }        };         // when button is pressed        b.setOnAction(event);         // add button        r.getChildren().add(b);        r.getChildren().add(l);         // create a scene        Scene sc = new Scene(r, 200, 200);         // set the scene        s.setScene(sc);         s.show();    }     public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:  
Output:  
ManasChhabra2
sweetyty
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java