Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJavaFX | ChoiceDialog

JavaFX | ChoiceDialog

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
 

  1. ChoiceDialog(): creates an empty choice dialog with no items.
  2. ChoiceDialog(T d, Collection choices) : creates a choice dialog with a set of items form the collection provided and a selected item
  3. 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

 

method explanation
getItems() returns all the available items of the choice box
getSelectedItem() returns the selected item of the dialog
setSelectedItem(T item) sets the selected item of the dialog
setContentText(String s) sets the content text of the dialog
setHeaderText(String s) sets the header textof the dialog

Below programs illustrate the ChoiceDialog class: 
 

  1. 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 it
import 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);
    }
}


  1. Output: 
     

  1. 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 choice
import 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);
    }
}


  1. Output: 
     

RELATED ARTICLES

Most Popular

Recent Comments