Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaFX | Alert with examples

  • Last Updated : 19 Apr, 2021

Alert is a part of JavaFX and it is a subclass of Dialog class. Alerts are some predefined dialogs that are used to show some information to the user. Alerts are basically of specific alert types: 
 

  • CONFIRMATION alert :The CONFIRMATION alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is seeking confirmation from the user.
  • WARNING alert :The WARNING alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is warning the user about some fact or action.
  • NONE alert :The NONE alert type has the effect of not setting any default properties in the Alert.
  • INFORMATION alert :The INFORMATION alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is informing the user of a piece of information.
  • ERROR alert :The ERROR alert type configures the Alert dialog to appear in a way that suggests that something has gone wrong.

An Alert contains 3 parts: 
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • The header
  • The content text
  • The confirmation buttons

Constructors of the class are: 
 

  1. Alert(Alert.AlertType a): Creates a new alert with a specified alert type.
  2. Alert(Alert.AlertType a, String c, ButtonType… b): Creates a new alert with a specified alert type, content and button type.

Commonly used methods: 



 

methodexplanation
getAlertType()set a specified alert type
setAlertType(Alert.AlertType a)set a specified alert type for the alert
getButtonTypes()Returns an ObservableList of all ButtonType instances that are currently set inside this Alert instance.
setContentText(String s)sets the context text for the alert
getContentText()returns the content text for the alert.

Below programs illustrate the Alert class: 
 

  1. Program to create alert of different types and display them: This program creates an alert which is of default type. The alert would be changed to different alert types when required. This program creates a Buttons indicated by the name b, b1, b2, b3. The buttons 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. When the buttons are pressed they will display the respective alerts associated with them and will set the respective alertType using the function setAlertType() function.
     

Java




// Java Program to create alert of different
// types and display them
 
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 Alert_1 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating alerts");
 
        // create a button
        Button b = new Button("Confirmation alert");
        Button b1 = new Button("error alert");
        Button b2 = new Button("Information alert");
        Button b3 = new Button("Warning alert");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a alert
        Alert a = new Alert(AlertType.NONE);
 
        // action event
        EventHandler<ActionEvent> event = new
                         EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.CONFIRMATION);
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event1 = new
                         EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.ERROR);
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event2 = new
                         EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.INFORMATION);
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event3 = new
                          EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.WARNING);
 
                // show the dialog
                a.show();
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
        b1.setOnAction(event1);
        b2.setOnAction(event2);
        b3.setOnAction(event3);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b2);
        r.getChildren().add(b3);
 
        // 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.  

  1.  

  1.  

  1. Program to create alert and set different alert types and button type and also set different content text: This program creates an alert which is of default type. the alert would be changed to different alert types when required. This program creates a Buttons indicated by the name b, b1, b2, b3, b4. The buttons 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. When the buttons are pressed they will display the respective alerts associated with them and will set the respective alertType using the function setAlertType() function. The content text would also be changed using the setContentText() method .we would create a new alert for the 4th button which is of default type, and also set a button type using the constructor of alert.
     

Java




// Java Program to create alert and set
// different alert types and button type
// and also set different content text
 
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 Alert_2 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating alerts");
 
        // create a button
        Button b = new Button("Confirmation alert");
        Button b1 = new Button("error alert");
        Button b2 = new Button("Information alert");
        Button b3 = new Button("Warning alert");
        Button b4 = new Button("none alert");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a alert
        Alert a = new Alert(AlertType.NONE);
 
        // action event
        EventHandler<ActionEvent> event = new
                          EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.CONFIRMATION);
 
                // set content text
                a.setContentText("ConfirmationDialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event1 = new
                          EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.ERROR);
 
                // set content text
                a.setContentText("error Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event2 = new
                          EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.INFORMATION);
 
                // set content text
                a.setContentText("Information Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event3 = new
                            EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.WARNING);
 
                // set content text
                a.setContentText("Warning Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler<ActionEvent> event4 = new
                               EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                Alert a1 = new Alert(AlertType.NONE,
                              "default Dialog",ButtonType.APPLY);
 
                // show the dialog
                a1.show();
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
        b1.setOnAction(event1);
        b2.setOnAction(event2);
        b3.setOnAction(event3);
        b4.setOnAction(event4);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b2);
        r.getChildren().add(b3);
        r.getChildren().add(b4);
 
        // 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.  

  1.  

  1.  

  1.  




My Personal Notes arrow_drop_up
Recommended Articles
Page :