Updated July 10, 2023
Introduction to JavaFX Alert
In JavaFX, an Alert box alerts the user about further processes. JavaFX Alert class is a subclass of the Dialog class. The alert box in javafx alerts the user about information, error messages, confirmation messages, and warning messages to let them know the user about what the exact dialog popup is about.
There are different ways to alert the user about what is happening next. That there are 5 Alert types to alert the user.
Types of Alert Box in JavaFX
Below are the points which explain the types of alert box in javaFX:
- None Alert: The None alert types do not set to any default properties.
- Information Alert: The Information alert type informs the user about content information or suggests to the user what is going on in the next process.
- Error Alert: The Error alert type shows the user where things went wrong or what the error is with the functionality.
- Confirmation Alert: The confirmation alert type asks the user for permission to move further. We have two options available: Yes or If we click. Yes, we have permitted them to move next step. If we click No, then do not move to the next step.
- Warning Alert: The Warning alert type warns the user about some fact or action. The warning does not disturb the further process, like an error.
Methods and Constructors in Alert Box
Below we can see the methods and constructor of the alert box:
Methods
- getAlertType(): Gives alert type.
- setAlertType(Alert.AlertType): Setting the alert type.
- getButtonTypes(): Get button type from the Observable list.
- setContentText(String s): Setting the content or text to alert the user about what is the dialog box.
- getContentText(): Gives the content text which we have set.
Constructors
- Alert(Alert.AlertType alertType): Create a new Alert object with an alert type parameter.
- Alert(Alert.AlertType alertType, String string, ButtonType… buttonType): Create a new Alert object with alert type, String, and Button type parameters.
How does Alert Box work in JavaFX?
The alert box in JavaFX mainly works on the value of the alert type, which is provided by the Alert(AlertType.VALUE) constructor. Accessing JavaFX features user-defined class must extend the Application
1. NONE Alert
Alert alertType=new Alert(AlertType.NONE);
2. INFORMATION Alert
Alert alertType=new Alert(AlertType.INFORMATION);
3. ERROR Alert
Alert alertType=new Alert(AlertType.ERROR);
4. CONFIRMATION Alert
Alert alertType=new Alert(AlertType.INFORMATION);
5. WARNING Alert
Alert alertType=new Alert(AlertType.WARNING);
How to Create Alert Box in Java FX?
Steps to Create Alert Boxes:
Step 1: Create an Alert type
Alert alertType=new Alert(AlertType.TYPE);
Step 2: Create a Pane or any other component.
TilePane tilePane=new TilePane ();
Step 3: Creating a scene means a screen to display output.
Scene screen = new Scene(tilePane, length, width);
Step 4: Adding Scene reference screen to the Stage:
stage.setScene(screen);
Step 5: Show stage reference with the show () method.
stage.show();
Examples of JavaFX Alert
Below are the examples of JavaFX Alert:
1. Information Alert
Code:
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
public class InformtionAlertType extends Application {
@Override
public void start(Stage outputStage) throws Exception {
Alert alert = new Alert(AlertType.INFORMATION);// line 1
alert.setTitle("Information Dialog Box");// line 2
alert.setHeaderText("This is header section to write heading");// line 3
alert.setContentText("This is body section to write some info!");// line 4
alert.showAndWait(); // line 5
}
public static void main(String args[]) {
launch(args); // line 0
}
}
Output:
Explanation of the above program: In the above code, line 0 calls the start method from internal JVM. Line 1 creates an information alert type. Line 2 sets the title to a dialog box. Line 3 sets the header text. Line 4 sets the body text of the dialog box. Line 5 shows the dialog box output. As you can see, the output information dialog box has a predefined image on the right end.
2. Error Alert
Code:
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
public class ErrorAlertType extends Application {
@Override
public void start(Stage outputStage) throws Exception {
Alert alert = new Alert(AlertType.ERROR);// line 1
alert.setTitle("Error Dialog Box");// line 2
alert.setHeaderText("ERROR HEADING");// line 3
alert.setContentText("I am proving what is the error exactly!");// line 4
alert.showAndWait(); // line 5
}
public static void main(String args[]) {
launch(args); // line 0
}
}
Output:
Explanation of the above program: In the above code, line 0 calls the start method from internal JVM. Line 1 creates an error alert type. Line 2 sets the title to the dialog box. Line 3 sets the header text. Line 4 sets the body text of the dialog box. Line 5 shows the dialog box output. As you can see, the output information dialog box has a predefined image on the right end.
3. Confirmation Alert
Code:
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
public class ConfirmationAlertType extends Application {
@Override
public void start(Stage outputStage) throws Exception {
Alert alert = new Alert(AlertType.CONFIRMATION);// line 1
alert.setTitle("Confirmation Dialog Box");// line 2
alert.setHeaderText("Please Confirm!");// line 3
alert.setContentText("Are you sure want to move further?!");// line 4
alert.showAndWait(); // line 5
}
public static void main(String args[]) {
launch(args); // line 0
}
}
Output:
Explanation of the above program: In the above code, line 0 calls the start method from internal JVM. Line 1 creates a confirmation alert type. Line 2 sets the title to the dialog box. Line 3 sets the header text. Line 4 sets the body text of the dialog box. Line 5 shows the dialog box output. As you can see, the output information dialog box has a predefined image on the right end.
4. Warning Alert
Code:
import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
public class WarningAlertType extends Application {
@Override
public void start(Stage outputStage) throws Exception {
Alert alert = new Alert(AlertType.WARNING);// line 1
alert.setTitle("Warning Dialog Box");// line 2
alert.setHeaderText("Warning!");// line 3
alert.setContentText("Some packages may be installed additionally!");// line 4
alert.showAndWait(); // line 5
}
public static void main(String args[]) {
launch(args); // line 0
}
}
Output:
Explanation of the above program: In the above code, line 0 calls the start method from internal JVM. Line 1 creates a warning alert type. Line 2 sets the title to the dialog box. Line 3 sets the header text. Line 4 sets the body text of the dialog box. Line 5 shows the dialog box output. As you can see, the output information dialog box has a predefined image on the right end.
Recommended Articles
This is a guide to JavaFX Alert. Here we discuss Syntax, methods, and constructors and how to create different examples of JavaFX Alert. You can also go through our other related articles to learn more –