Class Alert
- All Implemented Interfaces:
EventTarget
Dialog class, and provides support for a number
of pre-built dialog types that can be easily shown to users to prompt for a
response. Therefore, for many users, the Alert class is the most suited class
for their needs (as opposed to using Dialog directly). Alternatively,
users who want to prompt a user for text input or to make a choice from a list
of options would be better served by using TextInputDialog and
ChoiceDialog, respectively.
When creating an Alert instance, users must pass in an Alert.AlertType
enumeration value. It is by passing in this value that the Alert instance will
configure itself appropriately (by setting default values for many of the
Dialog properties, including title,
header, and graphic,
as well as the default buttons that are expected in
a dialog of the given type.
To instantiate (but not yet show) an Alert, simply use code such as the following:
Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure you want to format your system?");
Once an Alert is instantiated, we must show it. More often than not, alerts (and dialogs in general) are shown in a modal and blocking fashion. 'Modal' means that the dialog prevents user interaction with the owning application whilst it is showing, and 'blocking' means that code execution stops at the point in which the dialog is shown. This means that you can show a dialog, await the user response, and then continue running the code that directly follows the show call, giving developers the ability to immediately deal with the user input from the dialog (if relevant).
JavaFX dialogs are modal by default (you can change this via the
Dialog.initModality(javafx.stage.Modality) API). To specify whether you want
blocking or non-blocking dialogs, developers simply choose to call
Dialog.showAndWait() or Dialog.show() (respectively). By default most
developers should choose to use Dialog.showAndWait(), given the ease of
coding in these situations. Shown below is three code snippets, showing three
equally valid ways of showing the Alert dialog that was specified above:
Option 1: The 'traditional' approach
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
formatSystem();
}
Option 2: The traditional + Optional approach
alert.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
formatSystem();
}
});
Option 3: The fully lambda approach
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> formatSystem());
There is no better or worse option of the three listed above, so developers
are encouraged to work to their own style preferences. The purpose of showing
the above is to help introduce developers to the Optional API, which
is new in Java 8 and may be foreign to many developers.
- Since:
- JavaFX 8u40
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn enumeration containing the available, pre-built alert types that theAlertclass can use to pre-populate various properties. -
Property Summary
PropertiesTypePropertyDescriptionfinal ObjectProperty<Alert.AlertType> When creating an Alert instance, users must pass in anAlert.AlertTypeenumeration value.Properties declared in class Dialog
contentText, dialogPane, graphic, headerText, height, onCloseRequest, onHidden, onHiding, onShowing, onShown, resizable, resultConverter, result, showing, title, width, x, yTypePropertyDescriptionfinal StringPropertyA property representing the content text for the dialog pane.final ObjectProperty<DialogPane> The root node of the dialog, theDialogPanecontains all visual elements shown in the dialog.final ObjectProperty<Node> The dialog graphic, presented either in the header, if one is showing, or to the left of thecontent.final StringPropertyA property representing the header text for the dialog pane.final ReadOnlyDoublePropertyProperty representing the height of the dialog.final ObjectProperty<EventHandler<DialogEvent>> Called when there is an external request to close thisDialog.final ObjectProperty<EventHandler<DialogEvent>> Called just after the Dialog has been hidden.final ObjectProperty<EventHandler<DialogEvent>> Called just prior to the Dialog being hidden.final ObjectProperty<EventHandler<DialogEvent>> Called just prior to the Dialog being shown.final ObjectProperty<EventHandler<DialogEvent>> Called just after the Dialog is shown.final BooleanPropertyRepresents whether the dialog is resizable.final ObjectProperty<Callback<ButtonType, ButtonType>> API to convert theButtonTypethat the user clicked on into a result that can be returned via theresultproperty.final ObjectProperty<ButtonType> A property representing what has been returned from the dialog.final ReadOnlyBooleanPropertyRepresents whether the dialog is currently showing.final StringPropertyReturn the titleProperty of the dialog.final ReadOnlyDoublePropertyProperty representing the width of the dialog.final ReadOnlyDoublePropertyThe horizontal location of thisDialog.final ReadOnlyDoublePropertyThe vertical location of thisDialog. -
Constructor Summary
ConstructorsConstructorDescriptionAlert(Alert.AlertType alertType) Creates an alert with the given AlertType (refer to theAlert.AlertTypedocumentation for clarification over which one is most appropriate).Alert(Alert.AlertType alertType, String contentText, ButtonType... buttons) Creates an alert with the given contentText, ButtonTypes, and AlertType (refer to theAlert.AlertTypedocumentation for clarification over which one is most appropriate). -
Method Summary
Modifier and TypeMethodDescriptionfinal ObjectProperty<Alert.AlertType> When creating an Alert instance, users must pass in anAlert.AlertTypeenumeration value.final Alert.AlertTypeGets the value of thealertTypeproperty.final ObservableList<ButtonType> Returns anObservableListof allButtonTypeinstances that are currently set inside this Alert instance.final voidsetAlertType(Alert.AlertType alertType) Sets the value of thealertTypeproperty.Methods declared in class Dialog
addEventFilter, addEventHandler, buildEventDispatchChain, close, contentTextProperty, dialogPaneProperty, getContentText, getDialogPane, getGraphic, getHeaderText, getHeight, getModality, getOnCloseRequest, getOnHidden, getOnHiding, getOnShowing, getOnShown, getOwner, getResult, getResultConverter, getTitle, getWidth, getX, getY, graphicProperty, headerTextProperty, heightProperty, hide, initModality, initOwner, initStyle, isResizable, isShowing, onCloseRequestProperty, onHiddenProperty, onHidingProperty, onShowingProperty, onShownProperty, removeEventFilter, removeEventHandler, resizableProperty, resultConverterProperty, resultProperty, setContentText, setDialogPane, setGraphic, setHeaderText, setHeight, setOnCloseRequest, setOnHidden, setOnHiding, setOnShowing, setOnShown, setResizable, setResult, setResultConverter, setTitle, setWidth, setX, setY, show, showAndWait, showingProperty, titleProperty, widthProperty, xProperty, yPropertyModifier and TypeMethodDescriptionfinal <E extends Event>
voidaddEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Registers an event filter for this target.final <E extends Event>
voidaddEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event handler for this target.Construct an event dispatch chain for this target.final voidclose()Closes thisDialog.final StringPropertyA property representing the content text for the dialog pane.final ObjectProperty<DialogPane> The root node of the dialog, theDialogPanecontains all visual elements shown in the dialog.final StringReturns the currently-set content text for this DialogPane.final DialogPaneGets the value of thedialogPaneproperty.final NodeGets the value of thegraphicproperty.final StringReturns the currently-set header text for this DialogPane.final doubleReturns the height of the dialog.final ModalityRetrieves the modality attribute for this dialog.final EventHandler<DialogEvent> Gets the value of theonCloseRequestproperty.final EventHandler<DialogEvent> Gets the value of theonHiddenproperty.final EventHandler<DialogEvent> Gets the value of theonHidingproperty.final EventHandler<DialogEvent> Gets the value of theonShowingproperty.final EventHandler<DialogEvent> Gets the value of theonShownproperty.final WindowgetOwner()Retrieves the owner Window for this dialog, or null for an unowned dialog.final ButtonTypeGets the value of theresultproperty.final Callback<ButtonType, ButtonType> Gets the value of theresultConverterproperty.final StringgetTitle()Return the title of the dialog.final doublegetWidth()Returns the width of the dialog.final doublegetX()Gets the value of thexproperty.final doublegetY()Gets the value of theyproperty.final ObjectProperty<Node> The dialog graphic, presented either in the header, if one is showing, or to the left of thecontent.final StringPropertyA property representing the header text for the dialog pane.final ReadOnlyDoublePropertyProperty representing the height of the dialog.final voidhide()Hides thisDialog.final voidinitModality(Modality modality) Specifies the modality for this dialog.final voidSpecifies the ownerWindowfor this dialog, or null for a top-level, unowned dialog.final voidinitStyle(StageStyle style) Specifies the style for this dialog.final booleanReturns whether or not the dialog is resizable.final booleanReturns whether or not the dialog is showing.final ObjectProperty<EventHandler<DialogEvent>> Called when there is an external request to close thisDialog.final ObjectProperty<EventHandler<DialogEvent>> Called just after the Dialog has been hidden.final ObjectProperty<EventHandler<DialogEvent>> Called just prior to the Dialog being hidden.final ObjectProperty<EventHandler<DialogEvent>> Called just prior to the Dialog being shown.final ObjectProperty<EventHandler<DialogEvent>> Called just after the Dialog is shown.final <E extends Event>
voidremoveEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Unregisters a previously registered event filter from this target.final <E extends Event>
voidremoveEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Unregisters a previously registered event handler from this target.final BooleanPropertyRepresents whether the dialog is resizable.final ObjectProperty<Callback<ButtonType, ButtonType>> API to convert theButtonTypethat the user clicked on into a result that can be returned via theresultproperty.final ObjectProperty<ButtonType> A property representing what has been returned from the dialog.final voidsetContentText(String contentText) Sets the string to show in the dialog content area.final voidsetDialogPane(DialogPane value) Sets the value of thedialogPaneproperty.final voidsetGraphic(Node graphic) Sets the dialog graphic, which will be displayed either in the header, if one is showing, or to the left of thecontent.final voidsetHeaderText(String headerText) Sets the string to show in the dialog header area.final voidsetHeight(double height) Sets the height of the dialog.final voidsetOnCloseRequest(EventHandler<DialogEvent> value) Sets the value of theonCloseRequestproperty.final voidsetOnHidden(EventHandler<DialogEvent> value) Sets the value of theonHiddenproperty.final voidsetOnHiding(EventHandler<DialogEvent> value) Sets the value of theonHidingproperty.final voidsetOnShowing(EventHandler<DialogEvent> value) Sets the value of theonShowingproperty.final voidsetOnShown(EventHandler<DialogEvent> value) Sets the value of theonShownproperty.final voidsetResizable(boolean resizable) Sets whether the dialog can be resized by the user.final voidsetResult(ButtonType value) Sets the value of theresultproperty.final voidsetResultConverter(Callback<ButtonType, ButtonType> value) Sets the value of theresultConverterproperty.final voidChange the Title of the dialog.final voidsetWidth(double width) Sets the width of the dialog.final voidsetX(double x) Sets the value of thexproperty.final voidsetY(double y) Sets the value of theyproperty.final voidshow()Shows the dialog but does not wait for a user response (in other words, this brings up a non-blocking dialog).final Optional<ButtonType> Shows the dialog and waits for the user response (in other words, brings up a blocking dialog, with the returned value the users input).final ReadOnlyBooleanPropertyRepresents whether the dialog is currently showing.final StringPropertyReturn the titleProperty of the dialog.final ReadOnlyDoublePropertyProperty representing the width of the dialog.final ReadOnlyDoublePropertyThe horizontal location of thisDialog.final ReadOnlyDoublePropertyThe vertical location of thisDialog.
-
Property Details
-
alertType
When creating an Alert instance, users must pass in anAlert.AlertTypeenumeration value. It is by passing in this value that the Alert instance will configure itself appropriately (by setting default values for many of theDialogproperties, includingtitle,header, andgraphic, as well as the defaultbuttonsthat are expected in a dialog of the given type.- See Also:
-
-
Constructor Details
-
Alert
Creates an alert with the given AlertType (refer to theAlert.AlertTypedocumentation for clarification over which one is most appropriate).By passing in an AlertType, default values for the
title,headerText, andgraphicproperties are set, as well as the relevantbuttonsbeing installed. Once the Alert is instantiated, developers are able to modify the values of the alert as desired.It is important to note that the one property that does not have a default value set, and which therefore the developer must set, is the
content textproperty (or alternatively, the developer may callalert.getDialogPane().setContent(Node)if they want a more complex alert). If the contentText (or content) properties are not set, there is no useful information presented to end users.- Parameters:
alertType- an alert with the given AlertType
-
Alert
Creates an alert with the given contentText, ButtonTypes, and AlertType (refer to theAlert.AlertTypedocumentation for clarification over which one is most appropriate).By passing in a variable number of ButtonType arguments, the developer is directly overriding the default buttons that will be displayed in the dialog, replacing the pre-defined buttons with whatever is specified in the varargs array.
By passing in an AlertType, default values for the
title,headerText, andgraphicproperties are set. Once the Alert is instantiated, developers are able to modify the values of the alert as desired.- Parameters:
alertType- the alert typecontentText- the content textbuttons- the button types
-
-
Method Details
-
getAlertType
Gets the value of thealertTypeproperty.- Property description:
- When creating an Alert instance, users must pass in an
Alert.AlertTypeenumeration value. It is by passing in this value that the Alert instance will configure itself appropriately (by setting default values for many of theDialogproperties, includingtitle,header, andgraphic, as well as the defaultbuttonsthat are expected in a dialog of the given type. - Returns:
- the value of the
alertTypeproperty - See Also:
-
setAlertType
Sets the value of thealertTypeproperty.- Property description:
- When creating an Alert instance, users must pass in an
Alert.AlertTypeenumeration value. It is by passing in this value that the Alert instance will configure itself appropriately (by setting default values for many of theDialogproperties, includingtitle,header, andgraphic, as well as the defaultbuttonsthat are expected in a dialog of the given type. - Parameters:
alertType- the value for thealertTypeproperty- See Also:
-
alertTypeProperty
When creating an Alert instance, users must pass in anAlert.AlertTypeenumeration value. It is by passing in this value that the Alert instance will configure itself appropriately (by setting default values for many of theDialogproperties, includingtitle,header, andgraphic, as well as the defaultbuttonsthat are expected in a dialog of the given type.- Returns:
- the
alertTypeproperty - See Also:
-
getButtonTypes
Returns anObservableListof allButtonTypeinstances that are currently set inside this Alert instance. A ButtonType may either be one of the pre-defined types (e.g.ButtonType.OK), or it may be a custom type (created via theButtonType(String)orButtonType(String, javafx.scene.control.ButtonBar.ButtonData)constructors.Readers should refer to the
ButtonTypeclass documentation for more details, but at a high level, each ButtonType instance is converted to a Node (although most commonly aButton) via the (overridable)DialogPane.createButton(ButtonType)method onDialogPane.- Returns:
- an
ObservableListof allButtonTypeinstances that are currently set inside this Alert instance
-