JavaFX Alert Dialog Example With Pictures

JavaFX has 5 different alert types, each have their own different icon button, and functionality. You can specify an alert based on what your application need. For example if you want to have and alert to indicate user input error, you can then use an alert then specifying the alert type with  Alert.AlertType.ERROR.

And  these are 5 different types of JavaFX alert dialog:

1. Error Alert - usually to indicate an error

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText("Alert Message");;

2. Info Alert - usually use for information

Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("Alert Message");;

3. Confirmation Alert

Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("Alert Message");;

4. Warning Alert

Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setContentText("Alert Message");;

5. Empty Alert - an empty alert you can customize its behavior

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText("Alert Message");;

And this is the full code i wrote to demonstrate those alert in real Java code, with buttons to trigger the alert to be shown.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TestFx extends Application {
    public void start(Stage primaryStage) throws Exception {
        Stage stage = new Stage();
        BorderPane borderPane = new BorderPane() ;
        VBox vBox = new VBox();
        Insets insets = new Insets(20,20,20,20);
        HBox hBox  = new HBox();

        //for alert Error
        Button button = new Button("Show Alert Error");
            showAlert("This is JavaFX alert type Error", Alert.AlertType.ERROR);

        //for alert Information
        Button button1 = new Button("Show Alert Information");
            showAlert("This is JavaFX alert type Information", Alert.AlertType.INFORMATION);

        //for alert Confirmation
        Button button2 = new Button("Show Alert Confirmation");
            showAlert("This is JavaFX alert type Confirmation", Alert.AlertType.CONFIRMATION);

        //for alert None
        Button button3 = new Button("Show Alert None");
            showAlert("This is JavaFX alert type None", Alert.AlertType.NONE);

        //for alert Warning
        Button button4 = new Button("Show Alert Warning");
            showAlert("This is JavaFX alert type Warning", Alert.AlertType.WARNING);

        //add buttons to scene
        hBox.getChildren().addAll(button, button1, button2, button3, button4);
        vBox.getChildren().addAll( hBox );
        borderPane.setCenter( vBox );
        Scene scene=new Scene(borderPane, 500, 500);

    private void showAlert(String alertMessage, Alert.AlertType type){
        Alert alert = new Alert(type);

    public static void main(String[] args) {


Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Connect SSH Using PEM Certificate On Windows

Which Better WinZip vs WinRAR? Windows Zip and Unzip Tools

How To Create Spring Boot Project Using Netbeans

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously