Class WMessageBox


public class WMessageBox
extends WDialog
A standard dialog for confirmation or to get simple user input.

The messagebox shows a message in a dialog window, with a number of buttons. These buttons may be standard buttons, or customized.

A messagebox is (usually) modal, and can be instantiated synchronously or asynchronously.

When using a messagebox asynchronously, there is no API call that waits for the messagebox to be processed. Instead, the usage is similar to instantiating a WDialog (or any other widget). You need to connect to the buttonClicked() signal to interpret the result and delete the message box.

The synchronous use of a messagebox involves the use of the static WWidget.show() method, which blocks the current thread until the user has processed the messabebox. Since this uses the WDialog#exec(), it suffers from the same scalability issues as well as limitations. See documentation of WDialog for more details.

This will show a message box that looks like this:

Example of a WMessageBox (default)

Example of a WMessageBox (polished)

i18n

The strings used in the WMessageBox buttons can be translated by overriding the default values for the following localization keys:

  • Wt.WMessageBox.Abort: Abort
  • Wt.WMessageBox.Cancel: Cancel
  • Wt.WMessageBox.Ignore: Ignore
  • Wt.WMessageBox.No: No
  • Wt.WMessageBox.NoToAll: No To All
  • Wt.WMessageBox.Ok: Ok
  • Wt.WMessageBox.Retry: Retry
  • Wt.WMessageBox.Yes: Yes
  • Wt.WMessageBox.YesToAll: Yes to All
  • Constructor Details

    • WMessageBox

      public WMessageBox()
      Creates an empty message box.
    • WMessageBox

      public WMessageBox​(java.lang.CharSequence caption, java.lang.CharSequence text, Icon icon, java.util.EnumSet<StandardButton> buttons)
      Creates a message box with given caption, text, icon, and buttons.
    • WMessageBox

      public WMessageBox​(java.lang.CharSequence caption, java.lang.CharSequence text, Icon icon, StandardButton button, StandardButton... buttons)
      Creates a message box with given caption, text, icon, and buttons.

      Calls this(caption, text, icon, EnumSet.of(button, buttons))

  • Method Details

    • setText

      public void setText​(java.lang.CharSequence text)
      Sets the text for the message box.
    • getText

      public WString getText()
      Returns the message box text.
    • getTextWidget

      public WText getTextWidget()
      Returns the text widget.

      This may be useful to customize the style or layout of the displayed text.

    • setIcon

      public void setIcon​(Icon icon)
      Sets the icon.
    • getIcon

      public Icon getIcon()
      Returns the icon.
    • addButton

      public void addButton​(WPushButton button, StandardButton result)
      Adds a custom button.

      When the button is clicked, the associated result will be returned.

    • addButton

      public WPushButton addButton​(java.lang.CharSequence text, StandardButton result)
      Adds a custom button with given text.

      When the button is clicked, the associated result will be returned.

    • addButton

      public WPushButton addButton​(StandardButton result)
      Adds a standard button.
    • setStandardButtons

      public void setStandardButtons​(java.util.EnumSet<StandardButton> buttons)
      Sets standard buttons for the message box.
    • setStandardButtons

      public final void setStandardButtons​(StandardButton button, StandardButton... buttons)
      Sets standard buttons for the message box.

      Calls setStandardButtons(EnumSet.of(button, buttons))

    • getStandardButtons

      public java.util.EnumSet<StandardButton> getStandardButtons()
      Returns the standard buttons.

      See Also:
      setStandardButtons(EnumSet buttons), addButton(WPushButton button, StandardButton result)
    • getButtons

      public java.util.List<WPushButton> getButtons()
      Returns the buttons.
    • getButton

      public WPushButton getButton​(StandardButton b)
      Returns the button widget for the given standard button.

      Returns null if the button isn't in the message box.

      This may be useful to customize the style or layout of the button.

    • setDefaultButton

      public void setDefaultButton​(WPushButton button)
      Sets the button as the default button.

      The default button is pressed when the user presses enter. Only one button can be the default button.

      If no default button is set, JWt will take a button that is associated with a StandardButton.Ok or StandardButton.Yes result.

    • setDefaultButton

      public void setDefaultButton​(StandardButton button)
      Sets the button as the default button.

      The default button is pressed when the user presses enter. Only one button can be the default button.

      The default value is 0 (no default button).

    • getDefaultButton

      public WPushButton getDefaultButton()
      Returns the default button.

      See Also:
      setDefaultButton(WPushButton button)
    • setEscapeButton

      public void setEscapeButton​(WPushButton button)
      Sets the escape button.

      The escape button is pressed when the user presses escapes.

      If no escape button is set, JWt will take a button that is associated with a StandardButton.Cancel or StandardButton.No result.

    • setEscapeButton

      public void setEscapeButton​(StandardButton button)
      Sets the escape button.

      The escape button is pressed when the user presses escapes.

      If no escape button is set, JWt will take a button that is associated with a StandardButton.Cancel or StandardButton.No result.

    • getEscapeButton

      public WPushButton getEscapeButton()
      Returns the escape button.

      See Also:
      setEscapeButton(WPushButton button)
    • getButtonResult

      public StandardButton getButtonResult()
      Returns the result of this message box.

      This value is only defined after the dialog is finished.

    • show

      public static StandardButton show​(java.lang.CharSequence caption, java.lang.CharSequence text, java.util.EnumSet<StandardButton> buttons, WAnimation animation)
      Convenience method to show a message box, blocking the current thread.

      Show a message box, blocking the current thread until the message box is closed, and return the result. The use of this method is not recommended since it uses WDialog#exec(). See documentation of WDialog for detailed information.

      This functionality is only available on Servlet 3.0 compatible servlet containers.

    • show

      public static final StandardButton show​(java.lang.CharSequence caption, java.lang.CharSequence text, java.util.EnumSet<StandardButton> buttons)
      Convenience method to show a message box, blocking the current thread.

      Returns show(caption, text, buttons, new WAnimation())

    • buttonClicked

      public Signal1<StandardButton> buttonClicked()
      Signal emitted when a button is clicked.
    • setHidden

      public void setHidden​(boolean hidden, WAnimation animation)
      Description copied from class: WWidget
      Hides or shows the widget.

      Hides or show the widget (including all its descendant widgets). When setting hidden = false, this widget and all descendant widgets that are not hidden will be shown. A widget is only visible if it and all its ancestors in the widget tree are visible, which may be checked using isVisible().

      Overrides:
      setHidden in class WDialog