Confirm Dialog
A ConfirmDialog
is a modal dialog designed to allow the user to choose one of a set of up to 3 options. The dialog blocks app execution until the user interacts with it or it closes due to a timeout.
ConfirmDialog.Result result = OptionDialog.showConfirmDialog(
"Do you confirm?",
"Confirmation",
ConfirmDialog.OptionType.OK_CANCEL,
ConfirmDialog.MessageType.QUESTION);
Usages
The ConfirmDialog
provides a way to ask users for confirmation or to choose between multiple options, such as Yes/No
or OK/Cancel
, ensuring that they acknowledge and confirm their actions.
Show Code
- Java
Types
Option type
The ConfirmDialog
supports the following option types, which determine the buttons displayed in the dialog:
OK
: Displays anOK
button.OK_CANCEL
: DisplaysOK
andCancel
buttons.ABORT_RETRY_IGNORE
: DisplaysAbort
,Retry
, andIgnore
buttons.YES_NO_CANCEL
: DisplaysYes
,No
, andCancel
buttons.YES_NO
: DisplaysYes
andNo
buttons.RETRY_CANCEL
: DisplaysRetry
andCancel
buttons.CUSTOM
: Displays custom buttons as specified.
Message type
The ConfirmDialog
supports the following message types. When you configures a type, The dialog displays an icon beside the message, and the dialog's theme updates according to the webforJ design system rules.
PLAIN
: Displays the message without an icon, using the default theme.ERROR
: Displays an error icon next to the message with the error theme applied.QUESTION
: Displays a question mark icon beside the message, using the primary theme.WARNING
: Displays a warning icon next to the message with the warning theme applied.INFO
: Displays an info icon beside the message, using the info theme.
In the following sample, the code configures a confirm dialog of type CUSTOM
with a custom title and message.
Show Code
- Java
Result
The ConfirmDialog
returns a result based on the user's interaction with the dialog. This result indicates which button the user clicked or if the dialog was dismissed due to a timeout.
The result will be returned from the show()
method, or the equivalent OptionDialog
method as shown below.
The ConfirmDialog.Result
enum includes the following possible results:
OK
: The user clicked theOK
button.CANCEL
: The user clicked theCANCEL
button.YES
: The user clicked theYES
button.NO
: The user clicked theNO
button.ABORT
: The user clicked theABORT
button.RETRY
: The user clicked theRETRY
button.IGNORE
: The user clicked theIGNORE
button.FIRST_CUSTOM_BUTTON
: The user clicked the first custom buttonSECOND_CUSTOM_BUTTON
: The user clicked the second custom buttonTHIRD_CUSTOM_BUTTON
: The user clicked the third custom buttonTIMEOUT
: The dialog timeouts.UNKNOWN
: An unknown result, typically used as a default or error state.
if (result == ConfirmDialog.Result.FIRST_CUSTOM_BUTTON) {
OptionDialog.showMessageDialog("Changes discarded", "Discarded", "Got it");
} else {
OptionDialog.showMessageDialog(
"Changes saved", "Saved", "Got it", MessageDialog.MessageType.INFO);
}
Default button
The ConfirmDialog
allows you to specify a default button that is preselected when the dialog is displayed. This enhances the user experience by providing a suggested action that can be quickly confirmed by pressing the Enter key.
ConfirmDialog dialog = new ConfirmDialog(
"Are you sure?", "Confirm", ConfirmDialog.OptionType.YES_NO);
dialog.setDefaultButton(Button.SECOND); // second button
dialog.show();
Buttons text
You can configure the text of the buttons using the setButtonText(ConfirmDialog.Button button, String text)
method.
ConfirmDialog dialog = new ConfirmDialog(
"Are you sure?", "Confirm", ConfirmDialog.OptionType.CUSTOM);
dialog.setButtonText(ConfirmDialog.Button.FIRST, "Absolutely");
dialog.setButtonText(ConfirmDialog.Button.SECOND, "Nope");
dialog.show();
HTML processing
By default, the confirm dialog processes and renders HTML content. You can turn off this feature by configuring it to display raw text instead.
ConfirmDialog dialog = new ConfirmDialog(
"<b>Are you sure?</b>", "Confirm",
ConfirmDialog.OptionType.YES_NO, ConfirmDialog.MessageType.QUESTION);
dialog.setRawText(true);
dialog.show();
Timeout
The ConfirmDialog
allows you to set a timeout duration after which the dialog automatically closes. This feature is useful for non-critical confirmations or actions that don't require the user's immediate interaction.
You can configure the timeout for the dialog using the setTimeout(int timeout)
method. The timeout duration is in seconds. If the specified time elapses without any user interaction, the dialog closes automatically.
ConfirmDialog dialog = new ConfirmDialog(
"Are you sure?", "Confirm", ConfirmDialog.OptionType.YES_NO);
dialog.setDefaultButton(Button.SECOND);
dialog.setTimeout(3);
ConfirmDialog.Result result = dialog.show();
switch (result) {
case TIMEOUT:
OptionDialog.showMessageDialog(
"You took too long to decide", "Timeout", "Got it",
MessageDialog.MessageType.WARNING);
break;
case YES:
OptionDialog.showMessageDialog(
"You clicked Yes", "Yes", "Got it",
MessageDialog.MessageType.INFO);
break;
default:
OptionDialog.showMessageDialog(
"You clicked No", "No", "Got it",
MessageDialog.MessageType.INFO);
break;
}
Best practices
- Clear and Concise Prompts: Ensure the prompt message clearly explains what action the user is being asked to confirm. Avoid ambiguity.
- Appropriate Option Types: Choose option types that match the context of the action. For simple yes/no decisions, use straightforward options. For more complex scenarios, provide additional buttons like "Cancel" to allow users to back out without making a choice.
- Logical Default Button: Set a default button that aligns with the most likely or recommended user action to streamline decision-making.
- Consistent Theming: Align the dialog and button themes with your app's design for a cohesive user experience.
- Judicious Use of Timeout: Set timeouts for non-critical confirmations, ensuring users have enough time to read and understand the prompt.
- Minimize Overuse: Use confirm dialogs sparingly to avoid user frustration. Reserve them for critical actions requiring explicit user confirmation.
Styling
Shadow parts
These are the various parts of the shadow DOM for the component, which will be required when styling via CSS is desired.
Reflected attributes
The reflected attributes of a component will be shown as attributes in the rendered HTML element for the component in the DOM. This means that styling can be applied using these attributes.