JavaFX | Alert with examples
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:
- The header
- The content text
- The confirmation buttons
Constructors of the class are:
- Alert(Alert.AlertType a): Creates a new alert with a specified alert type.
- Alert(Alert.AlertType a, String c, ButtonType… b): Creates a new alert with a specified alert type, content and button type.
Commonly used methods:
| method | explanantion |
|---|---|
| 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:
- 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 Program to create alert of different// types and display themimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;publicclassAlert_1extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating alerts");// create a buttonButton b =newButton("Confirmation alert");Button b1 =newButton("error alert");Button b2 =newButton("Information alert");Button b3 =newButton("Warning alert");// create a tile paneTilePane r =newTilePane();// create a alertAlert a =newAlert(AlertType.NONE);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.CONFIRMATION);// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.ERROR);// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event2 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.INFORMATION);// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event3 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.WARNING);// show the dialoga.show();}};// when button is pressedb.setOnAction(event);b1.setOnAction(event1);b2.setOnAction(event2);b3.setOnAction(event3);// add buttonr.getChildren().add(b);r.getChildren().add(b1);r.getChildren().add(b2);r.getChildren().add(b3);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:




- 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 Program to create alert and set// different alert types and button type// and also set different content textimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.control.Alert.AlertType;publicclassAlert_2extendsApplication {// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating alerts");// create a buttonButton b =newButton("Confirmation alert");Button b1 =newButton("error alert");Button b2 =newButton("Information alert");Button b3 =newButton("Warning alert");Button b4 =newButton("none alert");// create a tile paneTilePane r =newTilePane();// create a alertAlert a =newAlert(AlertType.NONE);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.CONFIRMATION);// set content texta.setContentText("ConfirmationDialog");// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.ERROR);// set content texta.setContentText("error Dialog");// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event2 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.INFORMATION);// set content texta.setContentText("Information Dialog");// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event3 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set alert typea.setAlertType(AlertType.WARNING);// set content texta.setContentText("Warninig Dialog");// show the dialoga.show();}};// action eventEventHandler<ActionEvent> event4 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){Alert a1 =newAlert(AlertType.NONE,"default Dialog",ButtonType.APPLY);// show the dialoga1.show();}};// when button is pressedb.setOnAction(event);b1.setOnAction(event1);b2.setOnAction(event2);b3.setOnAction(event3);b4.setOnAction(event4);// add buttonr.getChildren().add(b);r.getChildren().add(b1);r.getChildren().add(b2);r.getChildren().add(b3);r.getChildren().add(b4);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:





- https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.html
- https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.AlertType.html
- JavaFX | Arc with examples
- JavaFX | Box with examples
- JavaFX | Ellipse with examples
- JavaFX | Polyline with examples
- JavaFX | Circle with examples
- JavaFX | Cylinder with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | Button with examples
- JavaFX | ComboBox with examples
- JavaFX | RadioButton with examples
- JavaFx | ColorPicker with examples
- JavaFX | Polygon with examples
- JavaFX | DatePicker with examples
- JavaFX | PointLight with examples
- JavaFX | Line with examples
Note : The above programs might not run in an online editor please use an offline compiler.
Reference:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



