Class MessageDialog

All Implemented Interfaces:
IShellProvider
Direct Known Subclasses:
MessageDialogWithToggle

public class MessageDialog extends IconAndMessageDialog
A dialog for showing messages to the user.

This concrete dialog class can be instantiated as is, or further subclassed as required.

Note: This class does not use button IDs from IDialogConstants. Instead, the ID is the index of the button in the supplied array.

  • Field Details

  • Constructor Details

    • MessageDialog

      public MessageDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex)
      Create a message dialog. Note that the dialog will have no visual representation (no widgets) until it is told to open.

      The labels of the buttons to appear in the button bar are supplied in this constructor as an array. The open method will return the index of the label in this array corresponding to the button that was pressed to close the dialog.

      Note: If the dialog was dismissed without pressing a button (ESC key, close box, etc.) then SWT.DEFAULT is returned. Note that the open method blocks.

      As of 3.11 you can also use the other constructor which is based on varargs
      Parameters:
      parentShell - the parent shell, or null to create a top-level shell
      dialogTitle - the dialog title, or null if none
      dialogTitleImage - the dialog title image, or null if none
      dialogMessage - the dialog message
      dialogImageType - one of the following values:
      • MessageDialog.NONE for a dialog with no image
      • MessageDialog.ERROR for a dialog with an error image
      • MessageDialog.INFORMATION for a dialog with an information image
      • MessageDialog.QUESTION for a simple dialog without an icon
      • MessageDialog.WARNING for a dialog with a warning image
      dialogButtonLabels - an array of labels for the buttons in the button bar
      defaultIndex - the index in the button label array of the default button
    • MessageDialog

      public MessageDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, int defaultIndex, String... dialogButtonLabels)
      Create a message dialog. Note that the dialog will have no visual representation (no widgets) until it is told to open.

      The labels of the buttons to appear in the button bar are supplied in this constructor as a varargs of Strings. The open method will return the index of the label in this array corresponding to the button that was pressed to close the dialog.

      Note: If the dialog was dismissed without pressing a button (ESC key, close box, etc.) then SWT.DEFAULT is returned. Note that the open method blocks.

      Parameters:
      parentShell - the parent shell, or null to create a top-level shell
      dialogTitle - the dialog title, or null if none
      dialogTitleImage - the dialog title image, or null if none
      dialogMessage - the dialog message
      dialogImageType - one of the following values:
      • MessageDialog.NONE for a dialog with no image
      • MessageDialog.ERROR for a dialog with an error image
      • MessageDialog.INFORMATION for a dialog with an information image
      • MessageDialog.QUESTION for a simple dialog without an icon
      • MessageDialog.WARNING for a dialog with a warning image
      defaultIndex - the index in the button label array of the default button
      dialogButtonLabels - varargs of Strings for the button labels in the button bar
      Since:
      3.12
  • Method Details

    • buttonPressed

      protected void buttonPressed(int buttonId)
      Description copied from class: Dialog
      Notifies that this dialog's button with the given id has been pressed.

      The Dialog implementation of this framework method calls okPressed if the ok button is the pressed, and cancelPressed if the cancel button is the pressed. All other button presses are ignored. Subclasses may override to handle other buttons, but should call super.buttonPressed if the default handling of the ok and cancel buttons is desired.

      Overrides:
      buttonPressed in class Dialog
      Parameters:
      buttonId - the id of the button that was pressed (see IDialogConstants.*_ID constants)
    • configureShell

      protected void configureShell(Shell shell)
      Description copied from class: Window
      Configures the given shell in preparation for opening this window in it.

      The default implementation of this framework method sets the shell's image and gives it a grid layout. Subclasses may extend or reimplement.

      Overrides:
      configureShell in class Window
      Parameters:
      shell - the shell
    • createButtonsForButtonBar

      protected void createButtonsForButtonBar(Composite parent)
      Description copied from class: Dialog
      Adds buttons to this dialog's button bar.

      The Dialog implementation of this framework method adds standard ok and cancel buttons using the createButton framework method. These standard buttons will be accessible from getCancelButton, and getOKButton. Subclasses may override.

      Note: The common button order is: {other buttons}, OK, Cancel. On some platforms, Dialog.initializeBounds() will move the default button to the right.

      Overrides:
      createButtonsForButtonBar in class Dialog
      Parameters:
      parent - the button bar composite
    • createCustomArea

      protected Control createCustomArea(Composite parent)
      Creates and returns the contents of an area of the dialog which appears below the message and above the button bar.

      The default implementation of this framework method returns null. Subclasses may override.

      Parameters:
      parent - parent composite to contain the custom area
      Returns:
      the custom area control, or null
    • createDialogArea

      protected Control createDialogArea(Composite parent)
      This implementation of the Dialog framework method creates and lays out a composite and calls createMessageArea and createCustomArea to populate it. Subclasses should override createCustomArea to add contents below the message.
      Overrides:
      createDialogArea in class Dialog
      Parameters:
      parent - the parent composite to contain the dialog area
      Returns:
      the dialog area control
    • getButton

      protected Button getButton(int index)
      Gets a button in this dialog's button bar.
      Overrides:
      getButton in class Dialog
      Parameters:
      index - the index of the button in the dialog's button bar
      Returns:
      a button in the dialog's button bar, or null if there's no button with that index
      See Also:
    • getMinimumMessageWidth

      protected int getMinimumMessageWidth()
      Returns the minimum message area width in pixels This determines the minimum width of the dialog.

      Subclasses may override.

      Returns:
      the minimum message area width (in pixels)
    • handleShellCloseEvent

      protected void handleShellCloseEvent()
      Handle the shell close. Set the return code to SWT.DEFAULT as there has been no explicit close by the user.
      Overrides:
      handleShellCloseEvent in class Window
      See Also:
    • open

      public static boolean open(int kind, Shell parent, String title, String message, int style)
      Convenience method to open a simple dialog as specified by the kind flag.
      Parameters:
      kind - the kind of dialog to open, one of ERROR, INFORMATION, QUESTION, WARNING, CONFIRM, or QUESTION_WITH_CANCEL.
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
      style - SWT.NONE for a default dialog, or SWT.SHEET for a dialog with sheet behavior
      Returns:
      true if the user presses the OK or Yes button, false otherwise
      Since:
      3.5
    • open

      public static int open(int kind, Shell parent, String title, String message, int style, String... dialogButtonLabels)
      Method to open a simple dialog as specified by the kind flag. This method accepts varargs of String to set custom button labels. Use this method if you want to override the default labels.
      Parameters:
      kind - the kind of dialog to open, one of ERROR, INFORMATION, QUESTION, WARNING, CONFIRM, or QUESTION_WITH_CANCEL.
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
      style - SWT.NONE for a default dialog, or SWT.SHEET for a dialog with sheet behavior
      dialogButtonLabels - varargs of Strings for the button labels in the button bar
      Returns:
      the index of the button that was pressed.
      Since:
      3.13
    • openConfirm

      public static boolean openConfirm(Shell parent, String title, String message)
      Convenience method to open a simple confirm (OK/Cancel) dialog.
      Parameters:
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
      Returns:
      true if the user presses the OK button, false otherwise
    • openError

      public static void openError(Shell parent, String title, String message)
      Convenience method to open a standard error dialog.
      Parameters:
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
    • openInformation

      public static void openInformation(Shell parent, String title, String message)
      Convenience method to open a standard information dialog.
      Parameters:
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
    • openQuestion

      public static boolean openQuestion(Shell parent, String title, String message)
      Convenience method to open a simple Yes/No question dialog.
      Parameters:
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
      Returns:
      true if the user presses the Yes button, false otherwise
    • openWarning

      public static void openWarning(Shell parent, String title, String message)
      Convenience method to open a standard warning dialog.
      Parameters:
      parent - the parent shell of the dialog, or null if none
      title - the dialog's title, or null if none
      message - the message
    • createButton

      protected Button createButton(Composite parent, int id, String label, boolean defaultButton)
      Description copied from class: Dialog
      Creates a new button with the given id.

      The Dialog implementation of this framework method creates a standard push button, registers it for selection events including button presses, and registers default buttons with its shell. The button id is stored as the button's client data. If the button id is IDialogConstants.CANCEL_ID, the new button will be accessible from getCancelButton(). If the button id is IDialogConstants.OK_ID, the new button will be accesible from getOKButton(). Note that the parent's layout is assumed to be a GridLayout and the number of columns in this layout is incremented. Subclasses may override.

      Note: The common button order is: {other buttons}, OK, Cancel. On some platforms, Dialog.initializeBounds() will move the default button to the right.

      Overrides:
      createButton in class Dialog
      Parameters:
      parent - the parent composite
      id - the id of the button (see IDialogConstants.*_ID constants for standard dialog button ids)
      label - the label from the button
      defaultButton - true if the button is to be the default button, and false otherwise
      Returns:
      the new button
      See Also:
    • customShouldTakeFocus

      protected boolean customShouldTakeFocus()
      Return whether or not we should apply the workaround where we take focus for the default button or if that should be determined by the dialog. By default only return true if the custom area is a label or CLabel that cannot take focus.
      Returns:
      boolean
    • getImage

      public Image getImage()
      Description copied from class: IconAndMessageDialog
      Returns the image to display beside the message in this dialog.

      Subclasses may override.

      Specified by:
      getImage in class IconAndMessageDialog
      Returns:
      the image to display beside the message
    • getButtonLabels

      protected String[] getButtonLabels()
      An accessor for the labels to use on the buttons.
      Returns:
      The button labels to used; never null.
    • getDefaultButtonIndex

      protected int getDefaultButtonIndex()
      An accessor for the index of the default button in the button array.
      Returns:
      The default button index.
    • setButtons

      protected void setButtons(Button... buttons)
      A mutator for the array of buttons in the button bar.
      Parameters:
      buttons - The buttons in the button bar; must not be null.
    • setButtonLabels

      protected void setButtonLabels(String... buttonLabels)
      A mutator for the button labels.
      Parameters:
      buttonLabels - The button labels to use; must not be null.
    • open

      public int open()
      Description copied from class: Window
      Opens this window, creating it first if it has not yet been created.

      If this window has been configured to block on open ( setBlockOnOpen), this method waits until the window is closed by the end user, and then it returns the window's return code; otherwise, this method returns immediately. A window's return codes are window-specific, although two standard return codes are predefined: OK and CANCEL.

      Overrides:
      open in class Window
      Returns:
      the return code
      See Also: