Introduction to
JavaFX Dialogs
HASUNUMA Kenji
k.hasunuma@coppermine.jp
@khasunuma
Slide 2
Slide 2 text
Overview
Slide 3
Slide 3 text
JavaFX Dialogs (8u40)
• Alert - returns any ButtonType
• TextInputDialog - returns input text
• ChoiceDialog - returns choosen item
• Dialog - superclass of all dialogs
Slide 4
Slide 4 text
Alert - INFORMATION
Slide 5
Slide 5 text
Alert - WARNING
Slide 6
Slide 6 text
Alert - ERROR
Slide 7
Slide 7 text
Alert - CONFIRMATION
Slide 8
Slide 8 text
Alert - NONE
Slide 9
Slide 9 text
TextInputDialog
Slide 10
Slide 10 text
ChoiceDialog
Slide 11
Slide 11 text
Usage
Slide 12
Slide 12 text
Dialog on code
• Works instead of Stage.
• Often, exists into a controller as a part of
event procedures.
• showAndWait() - show and wait, then
returns a value as Optional.
• show() - show and wait, then returns no
value.
// Create a dialog
Alert alert = new Alert(INFORMATION);
!
// Setting properties
alert.setTitle("Title (INFORMATION)");
alert.setHeaderText("Header Text");
alert.setContentText("Content Text");
!
// Show and obtain the result
Optional result
= alert.showAndWait();
!
// Another way if the result is needless
// alert.show();
Slide 15
Slide 15 text
Result types
Dialog Result type
Alert
ButtonType
!
OK, CANCEL, CLOSE, APPLY,
FINISH, YES, NO, NEXT, PREVIOUS
TextInputDialog String
ChoiceDialog Type of choosen item
Slide 16
Slide 16 text
Tips
Slide 17
Slide 17 text
I want such a dialog...
How?
Slide 18
Slide 18 text
I want such a dialog...
How?
// Add following code
alert.setHeaderText(null);
Slide 19
Slide 19 text
/**
* Tips #2: the way to resize a dialog.
*/
public class SomeController {
@FXML
public void onClick(ActionEvent event) {
. . .
Alert alert = new Alert(INFORMATION);
// Using following methods to resize;
// setPrefWidth, setPrefHeight and setPrefSize
alert.getDialogPane().setPrefSize(400.0, 300.0);
Optional result = dialog.showAndWait();
/* Handle the result */
}
}
Slide 20
Slide 20 text
/**
* Tips #3: Simple dialog-based application.
*/
public class DialogApp extends Application {
@Overrides
public void start(Stage stage) throws Exception {
TextInputDialog dialog = new TextInputDialog();
/* setting the dialog */
Optional result = dialog.showAndWait();
/* handle the result */
}
public static void main(String...args) {
launch(args);
}
}
Slide 21
Slide 21 text
Conclusion
Slide 22
Slide 22 text
• The representation of JavaFX dialogs
are Dialog and their subclasses.
• JavaFX Dialog looks like Stage, but is
not Stage.
• At first create and configure a dialog,
and call showAndWait method.
Slide 23
Slide 23 text
Introduction to
JavaFX Dialogs
HASUNUMA Kenji
k.hasunuma@coppermine.jp
@khasunuma