1+ package application ;
2+
3+ import java .util .Optional ;
4+
5+ import javafx .application .Application ;
6+ import javafx .event .ActionEvent ;
7+ import javafx .event .EventHandler ;
8+ import javafx .geometry .Insets ;
9+ import javafx .geometry .Pos ;
10+ import javafx .scene .Scene ;
11+ import javafx .scene .control .Alert ;
12+ import javafx .scene .control .Alert .AlertType ;
13+ import javafx .scene .control .Button ;
14+ import javafx .scene .control .ButtonType ;
15+ import javafx .scene .control .Label ;
16+ import javafx .scene .control .TextField ;
17+ import javafx .scene .layout .HBox ;
18+ import javafx .scene .layout .VBox ;
19+ import javafx .scene .paint .Color ;
20+ import javafx .scene .text .Font ;
21+ import javafx .scene .text .FontWeight ;
22+ import javafx .scene .text .Text ;
23+ import javafx .stage .Stage ;
24+
25+ public class AlertDialogExample
26+ extends Application {
27+
28+ private TextField textFld ;
29+ private Text actionStatus ;
30+ private static final String titleTxt = "JavaFX Dialogs Example" ;
31+
32+ public static void main (final String [] args ) {
33+
34+ Application .launch (args );
35+ }
36+
37+ @ Override
38+ public void start (final Stage primaryStage ) {
39+
40+ primaryStage .setTitle (titleTxt );
41+
42+ // Window label
43+ final Label label = new Label ("Alert Dialogs" );
44+ label .setTextFill (Color .DARKBLUE );
45+ label .setFont (Font .font ("Calibri" , FontWeight .BOLD , 36 ));
46+ final HBox labelHb = new HBox ();
47+ labelHb .setAlignment (Pos .CENTER );
48+ labelHb .getChildren ().add (label );
49+
50+ // Text field
51+ final Label txtLbl = new Label ("A Text field:" );
52+ txtLbl .setFont (Font .font ("Calibri" , FontWeight .NORMAL , 20 ));
53+ this .textFld = new TextField ();
54+ this .textFld .setMinHeight (30.0 );
55+ this .textFld .setPromptText ("Enter some text and save." );
56+ this .textFld .setPrefColumnCount (15 );
57+ final HBox hbox = new HBox ();
58+ hbox .setSpacing (10 );
59+ hbox .getChildren ().addAll (txtLbl , this .textFld );
60+
61+ // Buttons
62+ final Button infobtn = new Button ("Info" );
63+ infobtn .setOnAction (new InfoButtonListener ());
64+ final Button savebtn = new Button ("Save" );
65+ savebtn .setOnAction (new SaveButtonListener ());
66+ final Button clearbtn = new Button ("Clear" );
67+ clearbtn .setOnAction (new ClearButtonListener ());
68+ final HBox buttonHb = new HBox (10 );
69+ buttonHb .setAlignment (Pos .CENTER );
70+ buttonHb .getChildren ().addAll (infobtn , savebtn , clearbtn );
71+
72+ // Status message text
73+ this .actionStatus = new Text ();
74+ this .actionStatus .setFont (Font .font ("Calibri" , FontWeight .NORMAL , 20 ));
75+ this .actionStatus .setFill (Color .FIREBRICK );
76+
77+ // Vbox
78+ final VBox vbox = new VBox (30 );
79+ vbox .setPadding (new Insets (25 , 25 , 25 , 25 ));;
80+ vbox .getChildren ().addAll (labelHb , hbox , buttonHb , this .actionStatus );
81+
82+ // Scene
83+ final Scene scene = new Scene (vbox , 500 , 300 ); // w x h
84+ primaryStage .setScene (scene );
85+ primaryStage .show ();
86+
87+ // Initial
88+ this .actionStatus .setText ("An example of Alert Dialogs. Enter some text and save." );
89+ infobtn .requestFocus ();
90+ }
91+
92+ private class InfoButtonListener implements EventHandler <ActionEvent > {
93+
94+ @ Override
95+ public void handle (final ActionEvent e ) {
96+
97+ // Show info alert dialog
98+
99+ final Alert alert = new Alert (AlertType .INFORMATION );
100+ alert .setTitle (titleTxt );
101+ alert .setHeaderText ("Information Alert" );
102+ final String s ="This is an example of JavaFX 8 Dialogs. " +
103+ "This is an Alert Dialog of Alert type - INFORMATION." + " \n \n " +
104+ "Other Alert types are: CONFIRMATION, ERROR, NONE and WARNING." ;
105+ alert .setContentText (s );
106+
107+ alert .show ();
108+ }
109+ }
110+
111+ private class SaveButtonListener implements EventHandler <ActionEvent > {
112+
113+ @ Override
114+ public void handle (final ActionEvent e ) {
115+
116+ // Show error alert dialog
117+
118+ final String txt = AlertDialogExample .this .textFld .getText ().trim ();
119+ String msg = "Text saved: " ;
120+ boolean valid = true ;
121+
122+ if ((txt .isEmpty ()) || (txt .length () < 5 )) {
123+
124+ valid = false ;
125+ final Alert alert = new Alert (AlertType .ERROR );
126+ alert .setTitle (titleTxt );
127+ final String s = "Text should be at least 5 characters long. " +
128+ "Enter valid text and save. " ;
129+ alert .setContentText (s );
130+
131+ alert .showAndWait ();
132+ msg = "Invalid text entered: " ;
133+ }
134+
135+ AlertDialogExample .this .actionStatus .setText (msg + txt );
136+
137+ if (! valid ) {
138+
139+ AlertDialogExample .this .textFld .requestFocus ();
140+ }
141+ }
142+ }
143+
144+ private class ClearButtonListener implements EventHandler <ActionEvent > {
145+
146+ @ Override
147+ public void handle (final ActionEvent e ) {
148+
149+ // Show confirm alert dialog
150+
151+ final Alert alert = new Alert (AlertType .CONFIRMATION );
152+ alert .setTitle (titleTxt );
153+ final String s = "Confirm to clear text in text field ! " ;
154+ alert .setContentText (s );
155+
156+ final Optional <ButtonType > result = alert .showAndWait ();
157+
158+ if ((result .isPresent ()) && (result .get () == ButtonType .OK )) {
159+
160+ AlertDialogExample .this .textFld .setText ("" );
161+ AlertDialogExample .this .actionStatus .setText ("An example of Alert Dialogs. Enter some text and save." );
162+ AlertDialogExample .this .textFld .requestFocus ();
163+ }
164+ }
165+ }
166+ }
0 commit comments