Java Reference
In-Depth Information
To create dialogs, JavaFX uses another instance of a
javafx.stage.Stage
class
to be displayed to the user. Similar to extending from a
JDialog
class in Swing, you
simply extend from a
Stage
class. You have the opportunity to pass in the owning
window in the constructor, which then calls the
initOwner()
method. The modal
state of the dialog box can be set using the
initModality()
method. The following
class extends from the
Stage
class, having a constructor initializing the owning stage
and modal state:
class MyDialog extends Stage {
public MyDialog(Stage owner, boolean modality, String
title) {
super();
initOwner(owner);
Modality m = modality
? Modality.APPLICATION_MODAL : Modality.NONE;
initModality(m);
...// The rest of the class
The rest of the code creates a scene (
Scene
) similar to the main application's
start()
method. Because login forms are pretty boring, we decided to create an an-
imation of a bouncing ball while the user is busy changing the password in the dialog
box. (You will see more about creating animation in future recipes.)
When the menu item for Change Password is selected, the
createLoginDia-
log
method checks to see if there is already an instance of
MyDialog
instantiated. If
so, it closes that instance and generates a new one. The newly created dialog is then
displayed. Similarly, the
RadioMenuItem
controls call the
createLoginDialog
method, passing different Boolean values to indicate whether the instantiated
MyDia-
log
instance should be set to modal or not. As mentioned earlier, the bouncy ball has
no bearing on the dialog; it's just added for effect.