Java FX: Advanced Controls (Part _03)
Lecture 06
Displaying Alert Dialogs
• Alert dialog: a small graphical window that pops up to display a message to the
user
• In JavaFX, you use the Alert class, which is in the [Link]
package to display any of the following types of alert dialogs:
• Information Alert – Displays a simple message with an OK button
• Confirmation Alert – Use to ask the user for confirmation. An OK button and a
Cancel button are displayed.
• Warning Alert – Displays a warning message. An OK button is displayed.
• Error Alert – Displays an error message. An OK button is displayed.
Alert Dialogs
The AlertType
• You use the Alert class and the AlertType enumeration together
• The following values are in the AlertType enumeration:
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]
Creating an Alert Dialog
• First, create an instance of the Alert class, passing an AlertType
enumeration to the constructor
Alert alert = new Alert([Link]);
[Link]("Delete File");
• Next, set the text of the message
[Link]("Are you sure you want to delete this file?!");
• Display the dialog
[Link]();
Determining the Type of Button Clicked (1 of 2)
• The Information, Warning, and Error alert dialogs each display an OK button
• The Confirmation alert dialog an OK and a Cancel button
• The showAndWait method returns an object that can be used to determine
which button was clicked
• The data type of the object that is returned from the showAndWait method is
Optional<ButtonType>
o ButtonType: Represents the button the user clicked in the dialog (OK,
CANCEL, YES, NO, etc.).
▪ Optional<T>: A wrapper that may or may not contain a value.
▪ This is useful because the user might close the dialog without
clicking a button (e.g., pressing the “X” button on the window).
▪ Optional ensures your program handles that case safely without
throwing a NullPointerException.
Determining the Type of Button Clicked (2 of 2)
1 Alert alert = new Alert([Link]);
2 [Link]("Are you sure?");
3 Optional<ButtonType> result = [Link]();
4
5 if ([Link]() == [Link])
6 {
7 // Code to execute when the OK button is clicked
8 }
9 else if ([Link]() == [Link])
10 {
11 // Code to execute when the Cancel button is clicked
12 }
Modal and Modeless Dialogs (1 of 2)
• The showAndWait method displays a modal dialog
• When the dialog is displayed, the calling code is blocked from execution until the
dialog is dismissed
• No statements appearing after the call to showAndWait will execute until the
dialog is closed
Modal and Modeless Dialogs (2 of 2)
• The show method displays a modeless dialog
• When the dialog is displayed, the statements appearing after the call to the show
method will continue to execute while the dialog is displayed
Customizing an Alert Dialog (1 of 2)
• You can use the setTitle and setHeaderText methods to customize
the text displayed in a dialog's title bar and header area
Alert alert = new Alert([Link]);
[Link]("Notification");
[Link]("Attention!");
[Link]("You have new mail");
[Link]();
Customizing an Alert Dialog (2 of 2)
• You can use the setGraphic method to customize the icon displayed in a dialog
Image image = new Image("file:[Link]");
ImageView icon = new ImageView(image);
Alert alert = new Alert([Link]);
[Link]("Notification");
[Link](icon);
[Link]("Attention!");
[Link]("You have new mail");
[Link]();
Demo..
The FileChooser Class (1 of 3)
• The FileChooser class (in
the [Link]
package) displays a dialog box
that allows the user to browse
for a file and select it.
• The class can display two
types of predefined dialog
boxes:
• open dialog box
• save dialog box
The FileChooser Class (2 of 3)
• First, create an instance of the FileChooser class
• Then, call either the showOpenDialog method, or the showSaveDialog
method
• With either method, you pass a reference to the application’s stage as an argument
FileChooser fileChooser = new FileChooser();
File selectedFile = [Link](primaryStage);
• using [Link] to define the following options for file
selection: All images, JPG, and PNG.
[Link]().addAll(
new [Link]("All Images", "*.*"),
new [Link]("JPG", "*.jpg"),
new [Link]("PNG", "*.png")
);
The FileChooser Class (3 of 3)
• The showOpenDialog and showSaveDialog methods return a File object (in
the [Link] package) containing information about the selected file.
• If the user does not select a file, the method returns null.
FileChooser fileChooser = new FileChooser();
[Link]("Open Resource File");
File selectedFile = [Link](primaryStage);
if (se1ectedFi1e ! = null)
{
String filename = [Link]();
[Link]("You selected“ + filename);
}
Demo
The ColorPicker Class (1 of 2)
• A ColorPicker is actually a special type of ComboBox. By default, it
shows the color as well as either the name of the color or the amount of
RGB within the color.
ColorPicker colorPicker = new ColorPicker([Link]);
[Link]().add(colorPicker);
[Link](new EventHandler()
{
public void handle(Event t) {
[Link]([Link]()); }
});
// To match CSS color format:
[Link]("-fx-background-color:
"+[Link]().toString().replace("0x", "#"));
The ColorPicker Class (2 of 2)
• Notice that we create the ColorPicker just like creating a
button, but we get to specify the initial value (i.e., a Color)
that it begins with (i.e., RED). The user can then select the
value and a drop-down box appears allowing the user to select
a different color
Displaying Multiple Windows (1 of 2)
• You can display multiple windows in a JavaFX application.
• Each window is a stage, with its own scene
Displaying Multiple Windows (2 of 2)
The process for creating and displaying a second window:
1. Create the controls that you want to display in the window.
2. Create a layout container of some type and add the controls to the layout
container.
3. Create a Scene object and add the layout container to the Scene
object.
4. Create a Stage object.
5. Add the Scene object to the stage.
6. Call the Stage object's showAndWait method to display the window
in modal fashion, or the show method to display the window modelessly.
Displaying Multiple Windows (1 of 2)
public void start(Stage primaryStage) {
// Main window
Button openSecondBtn = new Button("Open Second Window");
VBox mainLayout = new VBox(openSecondBtn);
[Link]([Link]);
Scene mainScene = new Scene(mainLayout, 300, 200);
[Link](mainScene);
[Link]("Main Window");
[Link]();
[Link](e -> {
Stage secondStage = new Stage();
[Link]("Second Window");
VBox secondLayout = new VBox(new Button("Hello from Second
Window"));
[Link]([Link]);
Scene secondScene = new Scene(secondLayout, 250, 150);
[Link](secondScene);