JavaFX prompt dialog box is divided into two types and file dialog, which prompts divided into the dialog and message box warning dialog box, an error dialog box to confirm four. These four dialogs use the Alert control expression, and differentiated by the type of dialog box, for example AlertType.INFORMATION indicates that the message box, AlertType.WARNIN a warning dialog box, AlertType.ERROR an error dialog box, AlertType.CONFIRMATION to confirm dialogue frame. In addition, Alert tool also provides the following methods to manipulate the dialog box:
setTitle: Settings dialog box title.
setHeaderText: Settings dialog text head.
setContentText: Set the contents of the text box.
show: display the dialog box.
showAndWait: display the dialog box and wait for the button to return. Return type of the method is Optional <ButtonType>, which is used to describe the confirmation dialog box is selected the OK button or a cancel button.
Next, to name a few tips are implementation example dialog box first message, the calling code of its examples are as follows:
Button btn1 = new Button ( "message dialog"); // Create a button btn1.setOnAction (new EventHandler <ActionEvent> ( ) {// set the button's click event @Override public void handle (ActionEvent arg0) {// handling the click event Alert alert = new Alert (Alert.AlertType.INFORMATION) ; // create a message dialog alert.setHeaderText ( "today's weather"); // set text input dialog // Setup dialog box content text alert.setContentText ( "white sunny to cloudy today, turn north southerly 2,3 interstage 4, the highest temperature of 28 ℃; night cloudy to overcast, southerly about 2 ° C for minimum temperature of 16."); Alert.show (); // display a dialog } }); . flowPane.getChildren () the Add (btn1); // add to the flow pane button
Run a test program code above, and click the button to pop up a dialog box as shown below, splats visible message box is embedded exclamation point circle.
Followed by a warning dialog box, it's the calling code examples are as follows:
Button btn2 = new Button ( "warning dialog"); // Create a button btn2.setOnAction (new EventHandler <ActionEvent> ( ) {// set the button's click event @Override public void handle (ActionEvent arg0) {// handling the click event alert alert = new alert (Alert.AlertType.WARNING) ; // create a warning dialog alert.setHeaderText ( "compilation warnings"); // set text input dialog // Setup dialog box content text alert.setContentText ( "you are not initialized in line 60 of the code variables may cause a null pointer exception."); Alert.show (); // display a dialog } }); . flowPane.getChildren () the Add ( btn2); // add to the flow pane button
Run a test program code above, and click the button to pop up a dialog box as shown below, suggesting icon visible warning dialog is embedded exclamation point triangular box.
Once again, the error dialog, it calls the code examples are as follows:
Button btn3 = new Button ( "error dialog"); // Create a button btn3.setOnAction (new EventHandler <ActionEvent> ( ) {// set the button's click event @Override public void handle (ActionEvent arg0) {// handling the click event Alert alert = new Alert (Alert.AlertType.ERROR) ; // create an error dialog alert.setHeaderText ( "fatal error"); // set text input dialog // Setup dialog box content text alert.setContentText ( "system is about to shut down, please hurry to save the file."); Alert.show (); // display the dialog box } }); . flowPane.getChildren () the Add (btn3); // to streaming window Add button on the grid
Run a test program code above, and click the button to pop up a dialog box as shown below, we can see the error dialog box prompt icon is embedded crosses the box with rounded corners.
Finally, a confirmation dialog, which show an alternative method showAndWait method, the calling code specific examples are as follows:
Button btn4 = new Button ( "confirmation dialog"); // Create a button btn4.setOnAction (new EventHandler <ActionEvent> ( ) {// set the button's click event @Override public void handle (ActionEvent arg0) {// handling the click event Alert alert = new Alert (Alert.AlertType.CONFIRMATION) ; // create a confirmation dialog alert.setHeaderText ( "tips"); // set text input dialog content // settings dialog text alert.setContentText ( "Dear user, you really want to uninstall I do?"); // display the dialog box, and wait for the button to return Optional <buttonType> buttonType = alert.showAndWait ( ); button type // judge returned is OK or cancel, and then were further processed accordingly if (buttonType.get (). getButtonData ( ). equals (ButtonBar.ButtonData.OK_DONE)) {// click the OK button OK_DONE label.setText ( "you selected" OK "button, although reluctantly, but only left."); } Else {// click the Cancel button CANCEL_CLOSE } } }); . flowPane.getChildren () the Add (btn4); // to streaming window Add button on the grid
Run a test program code above, and click the button to pop up a dialog box as shown below, visible confirmation prompt dialog icon is embedded in a question mark circle.
Click the OK button to confirm each and Cancel buttons on the dialog box, the dialog box disappears after window interface, respectively, as shown in the following two graphs.
In addition to this broad category suggesting dialog box, as well as file dialog FileChooser, it segments the Open dialog box and save the file as a file dialog box, two kinds of common method FileChooser described as follows:
setTitle: the title of the file dialog.
setInitialDirectory: Set the initial directory file dialog.
getExtensionFilters: get extended filter file dialog. Call filter method or add addAll ways to add a new file type filter.
showOpenDialog: File Open dialog box. This method returns a file object selected.
showOpenMultipleDialog: File Open dialog box and the dialog box to select multiple files at the same time support. This method returns a list of documents selected.
showSaveDialog: File Save dialog box is displayed. This method returns a file object to be saved, the file may or may not exist.
Then look at the use of scene files dialog now ready to open a picture, for processing the picture. In view of the image file that contains jpg, gif, bmp, png and many other formats, when you create a file type filter was added several major picture extensions, open the dialog box calling code example is the picture below:
Button btn1 = new Button ( "File Open dialog box"); // Create a button btn1.setOnAction (new EventHandler <ActionEvent> ( ) {// set the button's click event @Override public void handle (ActionEvent arg0) {/ / handling the click event FileChooser chooser = new FileChooser (); // create a file dialog chooser.setTitle ( "open file"); // set the file dialog title chooser.setInitialDirectory (new file ( "E: \\ ")); // initial directory of the file dialog // file to the file dialog box to add multiple types of filters . chooser.getExtensionFilters () the addAll ( new new FileChooser.ExtensionFilter (." all files "," * " ), new new FileChooser.ExtensionFilter ( "all images", "* .jpg", " * .gif", "* .bmp", "* .png")); // File Open dialog box and the dialog box to select multiple files simultaneously support label.setText ( "No files selected"); file file = chooser.showOpenDialog (stage); // display the File Open dialog box if (file == null) {// file object is empty, indicating no select any file } else {// file object is not empty, indicating the selection of a file label.setText ( "ready to open the file path is:" + file.getAbsolutePath ()); } } }); flowPane.getChildren () the Add (btn1);. // to flow on the Add button pane
Run the above code comprises a test program, click the button dialog box as shown in FIG.
Select a picture file in a directory in the file dialog, the dialog interface at this time is shown below.
Then click the OK button to return to the main program window interface shown below, you can see the main program to successfully get the full path to the file.
Save the file again to verify the process of using the dialog box, the desire to save a piece of text to a text file, then the code for the specific implementation of the dialog box as shown below:
Button btn2 = new Button ( "File Save dialog box"); // Create a button btn2.setOnAction (new EventHandler <ActionEvent> ( ) {// set the button's click event @Override public void handle (ActionEvent arg0) {/ / handling the click event FileChooser chooser = new FileChooser (); // create a file dialog chooser.setTitle ( "save files"); // set the file dialog title chooser.setInitialDirectory (new file ( "E: \\ ")); // set the initial directory file dialog // create a file type filter FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter (" text files (* .txt) "," * .txt "); // to the file dialog box to add file type filter chooser.getExtensionFilters () the Add (filter);. file file = chooser.showSaveDialog (Stage);// display the file save dialog if (file == null) {// file object is empty, indicating no select any file label.setText ( "No files selected"); } else {// file object is not empty, indicating the selection of a file label.setText ( "ready to save the file path is:" + file.getAbsolutePath ()); } } }); . flowPane.getChildren () the Add (btn2); // add to the flow pane button
Run the above code comprises a test program, click the button dialog box as shown in FIG.
In the dialog box, enter the file to the specified directory, and fill in the name of the file to be saved in the file name under the column of the dialog box, then the dialog box interface is shown below.
Then click the OK button to return to the main program window interface shown below, you can see the main program is also available to the full path to the file.
See more Java technology articles " Java Development Notes (order) List of chapters "