JavaFX Alert Dialogs Tutorial
In this blog tutorial, you will learn to use the JavaFX alert dialogs. Alert is a subclass of the Dialog class. JavaFX alert dialog is used to show some information to the user.
Types of Alert dialogs in JavaFX
-
Confirmation – This type of alert is used to confirm from the user in a way that suggests the content of the dialog is seeking confirmation from the user. This includes confirmation of both the Ok and Cancel buttons.
-
Warning – This type of alert is used to show a warning alert to inform the user about some fact of action.
-
None – This is a type of alert that has the effect of not setting any default properties in the Alert.
-
Information – This type of alert is used to show a piece of information.
-
Error – This type of alert is used to show a piece of information that has gone wrong.
Source code to show alert dialog in JavaFX
Confirmation Alert
Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Confirmation alert Dialog"); alert.setContentText("Please confirm"); alert.setHeaderText("Confirmation"); alert.showAndWait();
Output
Warning Alert
Alert alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("Warning Dialog"); alert.setContentText("This is a warning alert!"); alert.setHeaderText("Warning alert"); alert.showAndWait();
Output
None Alert type
Alert alert = new Alert(Alert.AlertType.NONE); alert.setTitle("None Alert type"); alert.setContentText("This is a none type alert!"); alert.setHeaderText(null); alert.getDialogPane().getButtonTypes().add(ButtonType.CLOSE); alert.showAndWait();
Output
Information Alert
Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Information Dialog"); alert.setContentText("This is an information alert!"); alert.setHeaderText("Information alert"); alert.showAndWait();
Output
Error Alert
Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Error Dialog"); alert.setContentText("This is an error alert!"); alert.setHeaderText("Error alert"); alert.showAndWait();
Output