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");
alert.show();

2. Info Alert - usually use for information

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

3. Confirmation Alert

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

4. Warning Alert

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

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

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

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 {
    @Override
    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);
        vBox.setPadding(insets);
        HBox hBox  = new HBox();

        //for alert Error
        Button button = new Button("Show Alert Error");
        button.setOnAction(action->{
            showAlert("This is JavaFX alert type Error", Alert.AlertType.ERROR);
        });

        //for alert Information
        Button button1 = new Button("Show Alert Information");
        button1.setOnAction(action->{
            showAlert("This is JavaFX alert type Information", Alert.AlertType.INFORMATION);
        });

        //for alert Confirmation
        Button button2 = new Button("Show Alert Confirmation");
        button2.setOnAction(action->{
            showAlert("This is JavaFX alert type Confirmation", Alert.AlertType.CONFIRMATION);
        });

        //for alert None
        Button button3 = new Button("Show Alert None");
        button3.setOnAction(action->{
            showAlert("This is JavaFX alert type None", Alert.AlertType.NONE);
        });

        //for alert Warning
        Button button4 = new Button("Show Alert Warning");
        button4.setOnAction(action->{
            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);
        stage.setScene(scene);
        stage.show();
    }

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

    public static void main(String[] args) {
        launch(args);
    }
}


Comments

Popular posts from this blog

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

Flixify Video Streaming Platform, Watch Movies and TV Show Online

ASUS Battery Health Charging Software To Make Your Battery Life Longer

How To Uninstall Angular CLI, The Angular Framework

JavaFX Combo Box with Search Autocomplete

Is Nox Player a Malware? Nox Android Emulator