Top Description Inners Fields Constructors Methods
javafx.scene.control

public Class Alert

extends Dialog<ButtonType>
Class Inheritance
Imports
java.lang.ref.WeakReference, java.util.ArrayList, .Arrays, .List, .Optional, com.sun.javafx.scene.control.skin.resources.ControlResources, javafx.beans.InvalidationListener, .NamedArg, javafx.beans.property.ObjectProperty, .SimpleObjectProperty, javafx.collections.ListChangeListener, .ObservableList

The Alert class subclasses the 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 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?");
Image of the Alert control

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 initModality(javafx.stage.Modality) API). To specify whether you want blocking or non-blocking dialogs, developers simply choose to call showAndWait() or show() (respectively). By default most developers should choose to use 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
Dialog, AlertType, TextInputDialog, ChoiceDialog

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
Alert.AlertType

An enumeration containing the available, pre-built alert types that the Alert class can use to pre-populate various properties.

Field Summary

Modifier and TypeField and Description
private final ObjectProperty<Alert.AlertType>
alertType

When creating an Alert instance, users must pass in an AlertType enumeration value.

private final ListChangeListener<ButtonType>
private WeakReference<DialogPane>
private boolean
private boolean
private boolean
private final InvalidationListener
private boolean
private final InvalidationListener
Inherited from javafx.scene.control.Dialog:
dialog

Constructor Summary

AccessConstructor and Description
public
Alert(Alert.AlertType
an alert with the given AlertType
alertType
)

Creates an alert with the given AlertType (refer to the AlertType documentation for clarification over which one is most appropriate).

public
Alert(Alert.AlertType
the alert type
alertType
,
String
the content text
contentText
,
ButtonType...
the button types
buttons
)

Creates an alert with the given contentText, ButtonTypes, and AlertType (refer to the AlertType documentation for clarification over which one is most appropriate).

Method Summary

Modifier and TypeMethod and Description
public final ObjectProperty<Alert.AlertType>
public final Alert.AlertType
public final ObservableList<ButtonType>

Returns:

an ObservableList of all ButtonType instances that are currently set inside this Alert instance
getButtonTypes
()

Returns an ObservableList of all ButtonType instances that are currently set inside this Alert instance.

public final void
private void
Inherited from javafx.scene.control.Dialog:
addEventFilteraddEventHandlerbuildEventDispatchChainclosecontentTextPropertydialogPanePropertygetContentTextgetDialogPanegetGraphicgetHeaderTextgetHeightgetModalitygetOnCloseRequestgetOnHiddengetOnHidinggetOnShowinggetOnShowngetOwnergetResultgetResultConvertergetTitlegetWidthgetXgetYgraphicPropertyheaderTextPropertyheightPropertyhideinitModalityinitOwnerinitStyleisResizableisShowingonCloseRequestPropertyonHiddenPropertyonHidingPropertyonShowingPropertyonShownPropertyremoveEventFilterremoveEventHandlerresizablePropertyresultConverterPropertyresultPropertysetContentTextsetDialogPanesetGraphicsetHeaderTextsetHeightsetOnCloseRequestsetOnHiddensetOnHidingsetOnShowingsetOnShownsetResizablesetResultsetResultAndClosesetResultConvertersetTitlesetWidthsetXsetYshowshowAndWaitshowingPropertytitlePropertywidthPropertyxPropertyyProperty

Field Detail

alertTypeback to summary
private final ObjectProperty<Alert.AlertType> alertType

When creating an Alert instance, users must pass in an 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.

buttonsListenerback to summary
private final ListChangeListener<ButtonType> buttonsListener
dialogPaneRefback to summary
private WeakReference<DialogPane> dialogPaneRef
hasCustomButtonsback to summary
private boolean hasCustomButtons
hasCustomHeaderTextback to summary
private boolean hasCustomHeaderText
hasCustomTitleback to summary
private boolean hasCustomTitle
headerTextListenerback to summary
private final InvalidationListener headerTextListener
installingDefaultsback to summary
private boolean installingDefaults
titleListenerback to summary
private final InvalidationListener titleListener

Constructor Detail

Alertback to summary
public Alert(Alert.AlertType alertType)

Creates an alert with the given AlertType (refer to the AlertType documentation for clarification over which one is most appropriate).

By passing in an AlertType, default values for the title, headerText, and graphic properties are set, as well as the relevant buttons being 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 text property (or alternatively, the developer may call alert.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:Alert.AlertType

an alert with the given AlertType

Alertback to summary
public Alert(Alert.AlertType alertType, String contentText, ButtonType... buttons)

Creates an alert with the given contentText, ButtonTypes, and AlertType (refer to the AlertType documentation 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, and graphic properties are set. Once the Alert is instantiated, developers are able to modify the values of the alert as desired.

Parameters
alertType:Alert.AlertType

the alert type

contentText:String

the content text

buttons:ButtonType[]

the button types

Method Detail

alertTypePropertyback to summary
public final ObjectProperty<Alert.AlertType> alertTypeProperty()
getAlertTypeback to summary
public final Alert.AlertType getAlertType()
getButtonTypesback to summary
public final ObservableList<ButtonType> getButtonTypes()

Returns an ObservableList of all ButtonType instances 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 the ButtonType#ButtonType(String) or ButtonType#ButtonType(String, javafx.scene.control.ButtonBar.ButtonData) constructors.

Readers should refer to the ButtonType class documentation for more details, but at a high level, each ButtonType instance is converted to a Node (although most commonly a Button) via the (overridable) DialogPane#createButton(ButtonType) method on DialogPane.

Returns:ObservableList<ButtonType>

an ObservableList of all ButtonType instances that are currently set inside this Alert instance

setAlertTypeback to summary
public final void setAlertType(Alert.AlertType alertType)
updateListenersback to summary
private void updateListeners()
javafx.scene.control back to summary

public final Enum Alert.AlertType

extends Enum<Alert.AlertType>
Class Inheritance

An enumeration containing the available, pre-built alert types that the Alert class can use to pre-populate various properties.
Since
JavaFX 8u40

Field Summary

Modifier and TypeField and Description
public static final Alert.AlertType
CONFIRMATION

The CONFIRMATION alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is seeking confirmation from the user.

public static final Alert.AlertType
ERROR

The ERROR alert type configures the Alert dialog to appear in a way that suggests that something has gone wrong.

public static final Alert.AlertType
INFORMATION

The INFORMATION alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is informing the user of a piece of information.

public static final Alert.AlertType
NONE

The NONE alert type has the effect of not setting any default properties in the Alert.

public static final Alert.AlertType
WARNING

The WARNING alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is warning the user about some fact or action.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Alert.AlertType
public static Alert.AlertType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CONFIRMATIONback to summary
public static final Alert.AlertType CONFIRMATION

The CONFIRMATION alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is seeking confirmation from the user. This includes a 'confirmation' image, an appropriate title and header, and both OK and Cancel buttons for the user to click on to dismiss the dialog.

ERRORback to summary
public static final Alert.AlertType ERROR

The ERROR alert type configures the Alert dialog to appear in a way that suggests that something has gone wrong. This includes an 'error' image, an appropriate title and header, and just an OK button for the user to click on to dismiss the dialog.

INFORMATIONback to summary
public static final Alert.AlertType INFORMATION

The INFORMATION alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is informing the user of a piece of information. This includes an 'information' image, an appropriate title and header, and just an OK button for the user to click on to dismiss the dialog.

NONEback to summary
public static final Alert.AlertType NONE

The NONE alert type has the effect of not setting any default properties in the Alert.

WARNINGback to summary
public static final Alert.AlertType WARNING

The WARNING alert type configures the Alert dialog to appear in a way that suggests the content of the dialog is warning the user about some fact or action. This includes a 'warning' image, an appropriate title and header, and just an OK button for the user to click on to dismiss the dialog.

Constructor Detail

AlertTypeback to summary
private AlertType()

Method Detail

valueOfback to summary
public static Alert.AlertType valueOf(String name)
valuesback to summary
public static Alert.AlertType[] values()