JavaFX Dialogs (Official) - Code - Makery.ch
JavaFX Dialogs (Official) - Code - Makery.ch
JavaFX Dialogs (Official) - Code - Makery.ch
bs
cr
ib
e
to
code.makery (/)
Up
da
te
s
Learning how to code.
Home (/)
Library (/library/)
Paths (/paths/)
Blog (/blog/)
About (/about/)
JavaFX 8u40 finally includes simple Dialogs and Alerts! I’ve been waiting for this since 2012!
In the meantime I wrote about how to use Dialogs in JavaFX 2 (/blog/javafx-2-dialogs/) and
later in JavaFX 8 with ControlsFX (/blog/javafx-8-dialogs/).
Now that they are available in the official JDK, let’s learn how to use them.
Prerequisites
To use the official JavaFX Dialogs you need JDK 8u40 or later.
Standard Dialogs
Information Dialog
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");
alert.showAndWait();
alert.showAndWait();
Warning Dialog
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Warning Dialog");
alert.setHeaderText("Look, a Warning Dialog");
alert.setContentText("Careful with the next step!");
alert.showAndWait();
Error Dialog
alert.showAndWait();
Exception Dialog
There is not a complete Exception Dialog out of the box. But we can easily provide TextArea
as expandable content.
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("Look, an Exception Dialog");
alert.setContentText("Could not find file blabla.txt!");
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
alert.showAndWait();
// The Java 8 way to get the response value (with lambda expression).
result.ifPresent(name -> System.out.println("Your name: " + name));
Note: The result.isPresent() will return false if the user cancelled the dialog.
Choice Dialog
// The Java 8 way to get the response value (with lambda expression).
result.ifPresent(letter -> System.out.println("Your choice: " + letter));
Note: The result.isPresent() will return false if the user didn’t choose anything or
cancelled the dialog.
dialog.getDialogPane().setContent(grid);
result.ifPresent(usernamePassword -> {
System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassw
});
In the current version it’s a bit cumbersome to get to the Dialog’s Stage to be able to set its
icon. Here is how:
dialog.initOwner(otherStage);
Minimal Decorations
Another option is to remove the icon and use only minimal window decorations.
dialog.initStyle(StageStyle.UTILITY);
Other Options
Setting the Owner
You can specify the owner Window for a dialog. If no owner or null is specified for the owner,
it is a top-level, unowned dialog.
dialog.initOwner(parentWindow);
dialog.initModality(Modality.NONE);
API Documentation
For more information on the Dialogs have a look at the JavaFX API docs:
Alert (http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.html)
Dialog (http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html)
TextInputDialog
(http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextInputDialog.html)
ChoiceDialog
(http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ChoiceDialog.html)
DialogPane
(http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/DialogPane.html)
Comments
221 Comments code.makery
1 Login
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Perla Z 5 years ago
This is awesome! Thank you!
8△ ▽ • Reply • Share ›
Waiting for the blog post or waiting for Oracle to finally put the dialogs in the
official JDK? :-)
△ ▽ • Reply • Share ›
* Why does the dialog need to stay open? If it's a long running operation you could close the
dialog and then show some progress bar.
* If you need more functionality then you might be better off by just implementing your own
Stage.
* If you still want to use the dialogs, you might need to extend the Dialog class. Have a look
at the Dialogs source here: https://bitbucket.org/contr...
1△ ▽ • Reply • Share ›
Now I found the following workaround by performing a lookup of the OK button and using
an action filter which consumes the button action event in case of validation errors:
dlg.showAndWait();
△ ▽ • Reply • Share ›
dialog.initStyle(StageStyle.UTILITY);
*****
In the final version the dialog should have the same icon as its owner. You can set the owner
window like this.
dialog.initOwner(otherStage);
I don't know if this already works now, because there was a bug report that was fixed about a
day ago. See https://javafx-jira.kenai.c...
△ ▽ • Reply • Share ›
Marco Jakob Mod > Marco Jakob • 5 years ago
I've added the styling and icon examples to the blog post above.
△ ▽ • Reply • Share ›
dialog.initOwner(stage);
dialog.setHeight(300.0);
dialog.setWidth(600.0);
dialog.setTitle("Adding new selector");
dialog.setHeaderText("test");
dialog.setContentText("text:");
dialog.getDialogPane().setHeight(...);
dialog.getDialogPane().setWidth(...);
△ ▽ • Reply • Share ›
ilxan > Marco Jakob • 5 years ago
Thanks Marco,
There is probably no way of getting rid of that last pesky x icon on the minimal decorations, I
suppose, shy of an UNDECORATED stage altogether? css or some other hack perhaps?
About positioning: I think the dialogs should automatically be centered relative the their
owner but I might be wrong. Try setting the owner and then using dialog.setX(...) and
dialog.setY(...);
△ ▽ • Reply • Share ›
dialog.getButtonTypes().setAll(buttonTypeCancel, buttonTypeOk);
△ ▽ • Reply • Share ›
https://bitbucket.org/contr...
Instead of looking at the ButtonData to see if it's OK_DONE, it's actually checking to
see if the button pressed is it's own reference for OK.
△ ▽ • Reply • Share ›
I don't know what their idea is about how this should be done properly. But
you could probably set your own result converter:
△ ▽ • Reply • Share ›
I hope they decide to change it so that it actually looks at the ButtonData and
not the reference. IMO, that would be the right way to handle it.
△ ▽ • Reply • Share ›
http://fxexperience.com/201...
△ ▽ • Reply • Share ›
Please, in the future file bugs in the JavaFX jira issue tracker. Thanks!
23 △ ▽ • Reply • Share ›
JC • 5 years ago
Is there a way of setting the parent stage? Like the old versions?
△ ▽ • Reply • Share ›
I have another query which I think is a bug (or my lack of understanding with
SceneBuilder - I am unable to configure a ChoiceBox with its items in SceneBuilder -
completely off topic but where should I address that query to?
Many thanks
-- Michael Ellis
1△ ▽ • Reply • Share ›
I also think SceneBuilder is very helpful and probably the best visual editor
I've seen in my Java career. It's great that your system administrators can use
it in such a way.
If you've found a bug in scene builder it's best if you report it here:
https://javafx-jira.kenai.com/
△ ▽ • Reply • Share ›
Is there a way how Alert could consume a KeyEvent? I have a simple login AnchorPane
(username/password/login button) which method onKey(KeyEvent event) is assigned to handle
released key to process the login.
private void initialize() {
anchorPane.setOnKeyReleased((event) -> onKey(event))
}
and
△ ▽ • Reply • Share ›
Hi Craig, thank you. I wouldn't bother learning Swing except if you have a project where you
are forced to use it. Otherwise, JavaFX is the present and future as I see it.
△ ▽ • Reply • Share ›
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy
Enter email
Get updates
Created by Marco Jakob (/about) • Licensed under Creative Commons Attribution 4.0
(https://creativecommons.org/licenses/by/4.0/)