Class ConfirmDialog

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.confirmdialog.ConfirmDialog
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasComponents, HasElement, HasEnabled, HasSize, HasStyle, Serializable

@Tag("vaadin-confirm-dialog") @NpmPackage(value="@vaadin/confirm-dialog", version="25.0.0-alpha16") @JsModule("@vaadin/confirm-dialog/src/vaadin-confirm-dialog.js") public class ConfirmDialog extends Component implements HasComponents, HasSize, HasStyle
Confirm Dialog is a modal Dialog used to confirm user actions.

Confirm Dialog consists of:

  • Title
  • Message
  • Footer
    • “Cancel” button
    • “Reject” button
    • “Confirm” button

Each Confirm Dialog should have a title and/or message. The “Confirm” button is shown by default, while the two other buttons are not (they must be explicitly enabled to be displayed).

Author:
Vaadin Ltd
See Also:
  • Constructor Details

  • Method Details

    • getWidth

      public String getWidth()
      Description copied from interface: HasSize
      Gets the width defined for the component.

      Note that this does not return the actual size of the component but the width which has been set using HasSize.setWidth(String).

      Specified by:
      getWidth in interface HasSize
      Returns:
      the width which has been set for the component
    • setWidth

      public void setWidth(String width)
      Sets the width of the component content area.

      The width should be in a format understood by the browser, e.g. "100px" or "2.5em" (Using relative unit, such as percentage, will lead to unexpected results).

      If the provided width value is null then width is removed.

      Specified by:
      setWidth in interface HasSize
      Parameters:
      width - the width to set, may be null
    • getHeight

      public String getHeight()
      Description copied from interface: HasSize
      Gets the height defined for the component.

      Note that this does not return the actual size of the component but the height which has been set using HasSize.setHeight(String).

      Specified by:
      getHeight in interface HasSize
      Returns:
      the height which has been set for the component
    • setHeight

      public void setHeight(String height)
      Sets the height of the component content area.

      The height should be in a format understood by the browser, e.g. "100px" or "2.5em" (Using relative unit, such as percentage, will lead to unexpected results).

      If the provided height value is null then height is removed.

      Specified by:
      setHeight in interface HasSize
      Parameters:
      height - the height to set, may be null
    • getStyle

      public Style getStyle()
      Description copied from interface: HasStyle
      Gets the style instance for managing inline styles for the element of this component.
      Specified by:
      getStyle in interface HasStyle
      Returns:
      the style object for the element, not null
      Throws:
      UnsupportedOperationException - ConfirmDialog does not support adding styles
    • setAriaDescribedBy

      public void setAriaDescribedBy(String describedBy)
      Sets the `aria-describedby` attribute of the dialog.

      By default, all elements inside the message area are linked through the `aria-describedby` attribute. However, there are cases where this can confuse screen reader users (e.g. the dialog may present a password confirmation form). For these cases, it's better to associate only the elements that will help describe the confirmation dialog through this API.

      To restore the generated value, pass `null` as argument

      Parameters:
      describedBy - the attribute value
    • getAriaDescribedBy

      public Optional<String> getAriaDescribedBy()
      Gets the `aria-describedby` attribute of the dialog.

      Note that this will only return a value if setAriaDescribedBy(String) was called before.

      Returns:
      an optional aria-describedby of the dialog, or an empty optional if no aria-describedby has been set
    • setCancelable

      public void setCancelable(boolean cancelable)
      Whether to show or hide Cancel button.
    • setRejectable

      public void setRejectable(boolean rejectable)
      Whether to show or hide Reject button.
    • setRejectButton

      public void setRejectButton(String buttonText, ComponentEventListener<ConfirmDialog.RejectEvent> rejectListener)
      Sets Reject button text and `reject` event listener. Makes Reject button visible
      Parameters:
      buttonText - the text inside Reject button
      rejectListener - the event listener for `reject` event
    • setRejectButton

      public void setRejectButton(String buttonText, ComponentEventListener<ConfirmDialog.RejectEvent> rejectListener, String theme)
      Sets Reject button text, `reject` event listener, Reject button theme. Makes Reject button visible
      Parameters:
      buttonText - the text inside Reject button
      rejectListener - the event listener for `reject` event
      theme - the theme applied for a Reject button
    • setRejectButton

      public void setRejectButton(Component component)
      Sets custom Reject button
      Parameters:
      component - the component to display instead of default Reject button
    • setCancelButton

      public void setCancelButton(String buttonText, ComponentEventListener<ConfirmDialog.CancelEvent> cancelListener)
      Sets Cancel button text and `cancel` event listener. Makes Cancel button visible
      Parameters:
      buttonText - the text inside Cancel button
      cancelListener - the event listener for `cancel` event
    • setCancelButton

      public void setCancelButton(String buttonText, ComponentEventListener<ConfirmDialog.CancelEvent> cancelListener, String theme)
      Sets Cancel button text, `cancel` event listener and Cancel button theme. Makes Cancel button visible
      Parameters:
      buttonText - the text inside Cancel button
      cancelListener - the event listener for `cancel` event
      theme - the theme applied for a Cancel button
    • setCancelButton

      public void setCancelButton(Component component)
      Sets custom cancel button
      Parameters:
      component - the component to display instead of default Cancel button
    • setConfirmButton

      public void setConfirmButton(String buttonText, ComponentEventListener<ConfirmDialog.ConfirmEvent> confirmListener)
      Sets Confirm button text and `confirm` event listener
      Parameters:
      buttonText - the text inside Confirm button
      confirmListener - the event listener for `confirm` event
    • setConfirmButton

      public void setConfirmButton(String buttonText, ComponentEventListener<ConfirmDialog.ConfirmEvent> confirmListener, String theme)
      Sets Confirm button text, `confirm` event listener and Confirm button theme
      Parameters:
      buttonText - the text inside Confirm button
      confirmListener - the event listener for `confirm` event
      theme - the theme applied for a Confirm button
    • setConfirmButton

      public void setConfirmButton(Component component)
      Sets custom confirm button
      Parameters:
      component - the component to display instead of default Confirm button
    • setText

      public void setText(String message)
      Sets confirmation message text
    • setText

      public void setText(Component component)
      Sets custom confirmation message
      Parameters:
      component - the component to display instead of default confirmation text node
    • setConfirmText

      public void setConfirmText(String confirmText)
      Sets Confirm button text
    • setConfirmButtonTheme

      public void setConfirmButtonTheme(String confirmTheme)
      Sets Confirm button theme
    • addConfirmListener

      public Registration addConfirmListener(ComponentEventListener<ConfirmDialog.ConfirmEvent> listener)
      Adds a listener for when the user clicks the Confirm button.

      Note: The event is fired before the dialog's closing animation has finished. When manually adding or removing the dialog to or from the UI, use the ConfirmDialog.ClosedEvent to wait with the removal until the animation has finished. When relying on the auto-add behavior by just calling open() or setOpened(boolean), this is not necessary.

      Parameters:
      listener - the listener to add
      Returns:
      a Registration for removing the event listener
    • setCancelText

      public void setCancelText(String cancelText)
      Sets Cancel button text
    • setCancelButtonTheme

      public void setCancelButtonTheme(String cancelTheme)
      Sets Cancel button theme
    • addCancelListener

      public Registration addCancelListener(ComponentEventListener<ConfirmDialog.CancelEvent> listener)
      Adds a listener for when the user clicks the Cancel button or presses Escape.

      Note: The event is fired before the dialog's closing animation has finished. When manually adding or removing the dialog to or from the UI, use the ConfirmDialog.ClosedEvent to wait with the removal until the animation has finished. When relying on the auto-add behavior by just calling open() or setOpened(boolean), this is not necessary.

      Parameters:
      listener - the listener to add
      Returns:
      a Registration for removing the event listener
    • setRejectText

      public void setRejectText(String rejectText)
      Sets Reject button text
    • setRejectButtonTheme

      public void setRejectButtonTheme(String rejectTheme)
      Sets Reject button theme
    • addRejectListener

      public Registration addRejectListener(ComponentEventListener<ConfirmDialog.RejectEvent> listener)
      Adds a listener for when the user clicks the Reject button.

      Note: The event is fired before the dialog's closing animation has finished. When manually adding or removing the dialog to or from the UI, use the ConfirmDialog.ClosedEvent to wait with the removal until the animation has finished. When relying on the auto-add behavior by just calling open() or setOpened(boolean), this is not necessary.

      Parameters:
      listener - the listener to add
      Returns:
      a Registration for removing the event listener
    • addClosedListener

      public Registration addClosedListener(ComponentEventListener<ConfirmDialog.ClosedEvent> listener)
      Add a lister for when the dialog's closing animation has finished. Can be used to remove the dialog from the UI afterward.
      Parameters:
      listener - the listener to add
      Returns:
      a Registration for removing the event listener
    • setHeader

      public void setHeader(String header)
      Sets confirmation dialog header text
    • setHeader

      public void setHeader(Component component)
      Sets confirmation dialog custom header
      Parameters:
      component - the component to display instead of default header text
    • open

      public void open()
      Opens the dialog.

      If a dialog was not added manually to a parent component, it will be automatically added to the UI when opened, and automatically removed from the UI when closed. Note that the dialog is then scoped to the UI, and not the current view. As such, when navigating away from a view, the dialog will still be opened or stay open. In order to close the dialog when navigating away from a view, it should either be explicitly added as a child to the view, or it should be explicitly closed when leaving the view.

    • close

      public void close()
      Closes the dialog.

      This automatically removes the dialog from the UI, unless it was manually added to a parent component.

    • isOpened

      public boolean isOpened()
    • setOpened

      public void setOpened(boolean opened)
      Opens or closes the dialog.

      If a dialog was not added manually to a parent component, it will be automatically added to the UI when opened, and automatically removed from the UI when closed. Note that the dialog is then scoped to the UI, and not the current view. As such, when navigating away from a view, the dialog will still be opened or stay open. In order to close the dialog when navigating away from a view, it should either be explicitly added as a child to the view, or it should be explicitly closed when leaving the view.

      Parameters:
      opened - true to open the confirm-dialog, false to close it
    • isCloseOnEsc

      public boolean isCloseOnEsc()
      Gets whether this dialog can be closed by hitting the esc-key or not.

      By default, the dialog is closable with esc.

      Returns:
      true if this dialog can be closed with the esc-key, false otherwise
    • setCloseOnEsc

      public void setCloseOnEsc(boolean closeOnEsc)
      Sets whether this dialog can be closed by hitting the esc-key or not.

      By default, the dialog is closable with esc.

      Parameters:
      closeOnEsc - true to enable closing this dialog with the esc-key, false to disable it
    • removeAll

      public void removeAll()
      Removes all contents from this component, this includes child components, text content as well as child elements that have been added directly to this component using the Element API. it also removes the children that were added only at the client-side.

      Removes all components from this component except elements that have slot attribute, such as header and buttons.

      Specified by:
      removeAll in interface HasComponents