public final class Dialogs extends Object
Dialog class
(which is what all of these pre-built dialogs use as well).
A dialog consists of a number of sections, and the pre-built dialogs in
this class modify these sections as required. Refer to the Dialog
class documentation for more detail, but a brief overview is provided in
the following section.
A dialog consists of the following sections:
This is more easily demonstrated in the diagram shown below:

To better explain the dialogs, here is a table showing the default look
of all available pre-built dialogs when run on Windows (the button placement
in dialogs uses the ButtonBar control, so the buttons vary in order
based on the operating system in which the dialog is shown):
Without Masthead |
With Masthead |
|
|---|---|---|
| Information | ![]() |
![]() |
| Confirmation | ![]() |
![]() |
| Warning | ![]() |
![]() |
| Error | ![]() |
![]() |
| Exception | ![]() |
![]() |
| Exception (Expanded) | ![]() |
![]() |
| Exception (new window) | ![]() |
|
| Text Input | ![]() |
![]() |
| Choice Input (ChoiceBox/ComboBox) |
![]() |
![]() |
| Command Link | ![]() |
![]() |
| Font Selector | ![]() |
|
| Progress | ![]() |
|
The ControlsFX dialogs API supports displaying dialogs with either a
consistent cross-platform titlebar area, or by using the titlebar of the users
operating system. All of the screenshots above are taken using the cross-platform
style, whereas the screenshots below are the same dialog code being rendered
using the users native platform titlebar. To enable this in the Dialogs
fluent API, simply call nativeTitleBar() when creating the dialog.
If you're using the Dialog class, you can specify that you want to
use the native titlebar as part of the
Dialog.Dialog(Object, String, boolean, boolean) constructor (where the
fourth parameter is used to represent whether to use the native titlebar or not).
Here are the screenshots of dialogs with their native title bars:
Platform |
Screenshot |
|---|---|
| Cross-Platform (default) | ![]() |
| Mac OS X | ![]() |
| Windows 8 | ![]() |
| Linux (Ubuntu) | ![]() |
The ControlsFX dialogs API supports a distinction between heavyweight and lightweight dialogs. In short, a heavyweight dialog is rendered in its own JavaFX window, allowing for it to appear outside the bounds of the application. This is the most common style of dialog, and is therefore the default behavior when creating dialogs in ControlsFX. However, in some case, lightweight dialogs make more sense, so the following paragraphs will detail when you might want to use lightweight dialogs.
Lightweight dialogs are rendered within the scenegraph (and can't leave
the window). Other than this limitation, lightweight dialogs otherwise render
exactly the same as heavyweight dialogs (except a lightweight dialog normally
inserts an opaque overlay into the scene so that the dialog sticks out
visually). Lightweight dialogs are commonly useful in environments where a
windowing system is unavailable (e.g. tablet devices), and also when you only
want to block execution (and access to) a portion of your user interface. For
example, you could create a lightweight dialog with an owner of a single
Tab in a TabPane, and this will only block on that one tab -
all other tabs will continue to be interactive and execute as per usual.
One limitation of lightweight dialogs is that it is not possible to use
the native titlebar feature. If you call both lightweight() and
nativeTitleBar(), the call to enable lightweight takes precedence
over the use of the native titlebar, so you will end up seeing what is shown
in the screenshot below (that is, a cross-platform-looking dialog that is
lightweight).
To make a dialog lightweight, you simply call lightweight() when
constructing the dialog using this Dialogs API. If you are using the
Dialog class instead, then the decision between heavyweight and
lightweight dialogs must be made in the
Dialog.Dialog(Object, String, boolean) constructor.
Shown below is a screenshot of a lightweight dialog whose owner is the
Tab. This means that whilst the first tab is blocked for input until the
dialog is dismissed by the user, the rest of the UI (including going to other
tabs) remains interactive):

The code below will setup and show a confirmation dialog:
Action response = Dialogs.create()
.owner( isOwnerSelected ? stage : null)
.title("You do want dialogs right?")
.masthead(isMastheadVisible() ? "Just Checkin'" : null)
.message( "I was a bit worried that you might not want them, so I wanted to double check.")
.showConfirm();
The most important point to note about the dialogs is that they are modal, which means that they stop the application code from progressing until the dialog is closed. Because of this, it is very easy to retrieve the users input: when the user closes the dialog (e.g. by clicking on one of the buttons), the dialog will be hidden, and their response will be returned from the show method that was called to bring the dialog up in the first place. In other words, following on from the code sample above, you might do the following:
if (response == Dialog.Actions.OK) {
// ... submit user input
} else {
// ... user cancelled, reset form to default
}
The following code is an example of setting up a CommandLink dialog:
List<CommandLink> links = Arrays.asList(
new CommandLink("Add a network that is in the range of this computer",
"This shows you a list of networks that are currently available and lets you connect to one."),
new CommandLink("Manually create a network profile",
"This creates a new network profile or locates an existing one and saves it on your computer"),
new CommandLink("Create an ad hoc network",
"This creates a temporary network for sharing files or and Internet connection"));
Action response = Dialogs.create()
.owner(cbSetOwner.isSelected() ? stage : null)
.title("Manually connect to wireless network")
.masthead(isMastheadVisible() ? "Manually connect to wireless network": null)
.message("How do you want to add a network?")
.showCommandLinks( links.get(1), links );Dialog,
Action,
Dialog.Actions,
AbstractAction| Modifier and Type | Class and Description |
|---|---|
static class |
Dialogs.CommandLink
Command Link class.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
USE_DEFAULT
USE_DEFAULT can be passed in to
title(String) and masthead(String) methods
to specify that the default text for the dialog should be used, where the default text is
specific to the type of dialog being shown. |
| Modifier and Type | Method and Description |
|---|---|
Dialogs |
actions(Action... actions)
Completely replaces standard actions with provided ones.
|
Dialogs |
actions(Collection<? extends Action> actions)
Completely replaces standard actions with provided ones.
|
static Dialogs |
create()
Creates the initial dialog
|
Dialogs |
lightweight()
Specifies that the dialog should become lightweight, which means it is
rendered within the scene graph (and can't leave the window).
|
Dialogs |
masthead(String masthead)
Assigns dialog's masthead
|
Dialogs |
message(String message)
Assigns dialog's instructions
|
Dialogs |
nativeTitleBar()
Specifies that the dialog should use the native title bar of the users
operating system rather than the custom cross-platform rendering used by
default.
|
Dialogs |
owner(Object owner)
Assigns the owner of the dialog.
|
<T> T |
showChoices(Collection<T> choices)
Show a dialog with one combobox filled with provided choices
|
<T> T |
showChoices(T... choices)
Show a dialog with one combobox filled with provided choices
|
<T> T |
showChoices(T defaultChoice,
Collection<T> choices)
Show a dialog with one combobox filled with provided choices.
|
Action |
showCommandLinks(Dialogs.CommandLink defaultCommandLink,
Dialogs.CommandLink... links)
Show a dialog filled with provided command links.
|
Action |
showCommandLinks(Dialogs.CommandLink defaultCommandLink,
List<Dialogs.CommandLink> links)
Show a dialog filled with provided command links.
|
Action |
showCommandLinks(List<Dialogs.CommandLink> links)
Show a dialog filled with provided command links.
|
Action |
showConfirm()
Shows confirmation dialog.
|
Action |
showError()
Show error dialog
|
Action |
showException(Throwable exception)
Shows exception dialog with expandable stack trace.
|
Action |
showExceptionInNewWindow(Throwable exception)
Shows exception dialog with a button to open the exception text in a
new window.
|
Font |
showFontSelector(Font font)
Show font selection dialog, allowing to manipulate font name, style and size.
|
void |
showInformation()
Shows information dialog.
|
String |
showTextInput()
Shows dialog with one text field
|
String |
showTextInput(String defaultValue)
Shows dialog with one text field
|
Action |
showWarning()
Shows warning dialog
|
void |
showWorkerProgress(Worker<?> worker)
|
Dialogs |
title(String title)
Assigns dialog's title
|
public static final String USE_DEFAULT
title(String) and masthead(String) methods
to specify that the default text for the dialog should be used, where the default text is
specific to the type of dialog being shown.public static Dialogs create()
public Dialogs owner(Object owner)
owner - The dialog owner.public Dialogs title(String title)
title - dialog titlepublic Dialogs message(String message)
message - dialog messagepublic Dialogs masthead(String masthead)
masthead - dialog mastheadpublic Dialogs actions(Collection<? extends Action> actions)
actions - new dialog actionspublic Dialogs actions(Action... actions)
actions - new dialog actionspublic Dialogs lightweight()
Tab in a
TabPane, and this will only block on that one tab - all other
tabs will continue to be interactive and execute as per usual.public Dialogs nativeTitleBar()
public void showInformation()
public Action showConfirm()
public Action showWarning()
public Action showError()
public Action showException(Throwable exception)
exception - exception to presentpublic Action showExceptionInNewWindow(Throwable exception)
exception - exception to presentpublic String showTextInput(String defaultValue)
defaultValue - text field default valuepublic String showTextInput()
public <T> T showChoices(T defaultChoice,
Collection<T> choices)
defaultChoice - default combobox selectionchoices - dialog choicespublic <T> T showChoices(Collection<T> choices)
choices - dialog choicespublic <T> T showChoices(T... choices)
choices - dialog choicespublic Action showCommandLinks(Dialogs.CommandLink defaultCommandLink, List<Dialogs.CommandLink> links)
defaultCommandLink - command is set to be default. Null means no defaultlinks - list of command links presented in specified sequencepublic Action showCommandLinks(List<Dialogs.CommandLink> links)
links - list of command links presented in specified sequencepublic Action showCommandLinks(Dialogs.CommandLink defaultCommandLink, Dialogs.CommandLink... links)
defaultCommandLink - command is set to be default. Null means no defaultlinks - command links presented in specified sequencepublic Font showFontSelector(Font font)
font - default font valuepublic void showWorkerProgress(Worker<?> worker)
Dialog which is attached to the given
Worker instance. The worker will be observed permanently and
the attached dialog will be shown and hidden as the worker starts and
completes. If the worker's state is
Worker.State.SCHEDULED or
Worker.State.RUNNING the dialog will be visible.
Worker.State.READY,
Worker.State.SUCCEEDED,
Worker.State.FAILED, or
Worker.State.CANCELLED, the dialog will be shown
when the worker's state changes to SUCCEDED or RUNNING.
If the worker has a state of SCHEDULED or RUNNING, the dialog will be
hidden when the worker's state changes to SUCCEEDED, FAILED, or CANCELLED.
All other changes in worker state will not cause the visibility of the
attached dialog to change. Note that if a worker is submitted with a
state of SCHEDULED or RUNNING the dialog will be shown immediately.
LightweightDialogs. If an attempt is
made to show more than one lightweight dialog at a time, an
IllegalArgumentException will be thrown. If several workers have
dialogs attached, care should be taken to ensure only one of those workers
is SCHEDULED or RUNNING at any given time.
IllegalArgumentException to be thrown. If a worker's completion
handler is going to cause another dialog to be shown, or is going to restart
a failed worker, Platform.runLater(Runnable) should be used to
ensure notification about the worker's completion is finished first. Doing
so makes sure the worker's attached dialog has been hidden before any new
dialogs are shown during completion handling.
Service<Void> service = getService();
service.setOnFailed(new EventHandler<WorkerStateEvent>() {
public void handle(WorkerStateEvent event) {
Platform.runLater(new Runnable() {
public void run() {
// Make sure the service state is still failed. If so, the
// call to shouldRetry() will show the user a dialog asking
// if they'd like to retry and will return true or false
// depending on the user's answer.
if(isFailed(service) && shouldRetry()) {
service.restart();
}
}
});
}
});
The above code is much cleaner with Java 8 syntax:
Service<Void> service = getService();
service.setOnFailed(event -> Platform.runLater(() -> {
if(isFailed(service) && shouldRetry()) {
service.restart();
}
}));