Class OptionDialog

All Implemented Interfaces:
ActionContextProvider, StatusListener, TaskListener
Direct Known Subclasses:
OkDialog

public class OptionDialog extends DialogComponentProvider
A utility class to easily show dialogs that require input from the user.

Option Dialogs

The primary type of dialog provided herein is the basic option dialog that allows the user to specify the buttons that appear on the dialog. By default, the given option text will appear as a button(s), followed by a Cancel button (you can call the showOptionNoCancelDialog(Component, String, String, String, String, int) methods if you do not want a Cancel button. To use this type of dialog you can use the various showOptionDialog* methods.

Each of the option dialog methods will return a result, which is a number indicating the choice made by the user. See each method for more details.

Data Input and Choice Dialogs

The methods listed here allow the user to either enter data from the keyboard or to choose from a pre-populated list of data.

Yes/No Dialogs

Finally, there are a series of methods that present Yes and No buttons in a dialog. There are versions that do and do not have a Cancel button.

Basic Message / Warning / Error Dialogs

If you would like to display a simple message to the user, but do not require input from the user, then you should use the various methods of Msg, such as Msg.showInfo(Object, Component, String, Object).

Note, the user will be unable to select any text shown in the message area of the dialog.

"Apply to All" / "Don't Show Again"

For more advanced input dialog usage, to include allowing the user to tell the dialog to remember a particular decision, or to apply a given choice to all future request, see OptionDialogBuilder.
See Also:
  • Field Details

  • Constructor Details

    • OptionDialog

      protected OptionDialog(String title, String message, int messageType, Icon icon)
      Construct a simple informational dialog with a single OK button.
      Parameters:
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      messageType - used to specify a default icon
      • ERROR_MESSAGE
      • INFORMATION_MESSAGE
      • WARNING_MESSAGE
      • QUESTION_MESSAGE
      • PLAIN_MESSAGE
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
    • OptionDialog

      protected OptionDialog(String title, String message, String option1, String option2, int messageType, Icon icon, boolean addCancel)
      Construct a simple two-option dialog.
      Parameters:
      title - the String to place in the dialog's title area.
      message - the message string explaining the user's option.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
      addCancel - true means add a Cancel button
    • OptionDialog

      protected OptionDialog(String title, String message, String option1, String option2, int messageType, Icon icon, boolean addCancel, String defaultButtonName)
      Construct a simple two-option dialog.
      Parameters:
      title - the String to place in the dialog's title area.
      message - the message string explaining the user's option.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
      addCancel - true means add a Cancel button
      defaultButtonName - The default button name
    • OptionDialog

      protected OptionDialog(String title, String message, String option1, int messageType, Icon icon)
      Construct a simple one-option dialog with a Cancel button.
      Parameters:
      title - the String to place in the dialog's title area.
      message - the message string explaining the user's option.
      option1 - The text to place on the first option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
    • OptionDialog

      protected OptionDialog(String title, String message, String option1, int messageType, Icon icon, String defaultButtonName)
      Construct a simple one-option dialog with a Cancel button.
      Parameters:
      title - the String to place in the dialog's title area.
      message - the message string explaining the user's option.
      option1 - The text to place on the first option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
      defaultButtonName - the name of the button to be made the default.
    • OptionDialog

      protected OptionDialog(String title, String message, String option1, String option2, String option3, int messageType, Icon icon, boolean addCancel)
  • Method Details

    • createTextPanel

      protected JPanel createTextPanel(String message)
    • getMessage

      public String getMessage()
      Returns the dialog's message to the user
      Returns:
      the message
    • show

      public int show()
    • show

      public int show(Component parent)
    • createBuilder

      public static OptionDialogBuilder createBuilder(String title, String message)
      A convenience method to create a OptionDialogBuilder
      Parameters:
      title - the dialog title
      message - the dialog message
      Returns:
      the builder
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1)
      Static helper method to easily display an single-option dialog. The dialog will remain until the user presses the Option1 button or the Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the option button.
      Returns:
      The options selected by the user. 1 if the option button is pressed or 0 if the operation is cancelled.
    • showOptionDialogWithCancelAsDefaultButton

      public static int showOptionDialogWithCancelAsDefaultButton(Component parent, String title, String message, String option1)
      Static helper method to easily display an single-option dialog. The dialog will remain until the user presses the Option1 button or the Cancel button.

      The dialog shown by this method will have the cancel button set as the default button so that an Enter key press will trigger a cancel action.

      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the option button.
      Returns:
      The options selected by the user. 1 if the option button is pressed or 0 if the operation is cancelled.
    • showOptionDialogWithCancelAsDefaultButton

      public static int showOptionDialogWithCancelAsDefaultButton(Component parent, String title, String message, String option1, int messageType)
      Static helper method to easily display an single-option dialog. The dialog will remain until the user presses the Option1 button or the Cancel button.

      The dialog shown by this method will have the cancel button set as the default button so that an Enter key press will trigger a cancel action.

      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      The options selected by the user. 1 if the option button is pressed or 0 if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, int messageType)
      Static helper method to easily display an single-option dialog. The dialog will remain until the user presses the Option1 button or the Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      The options selected by the user. 1 if the option button is pressed or 0 if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, int messageType, String defaultButtonName)
      Static helper method to easily display an single-option dialog. The dialog will remain until the user presses the Option1 button or the Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      defaultButtonName - the name of the button to be the default. Null will make the first button the default
      Returns:
      The options selected by the user. 1 if the option button is pressed or 0 if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, Icon icon)
      Static helper method to easily display an single-option dialog. The dialog will remain until the user presses the Option1 button or the Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the option button.
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
      Returns:
      The options selected by the user. 1 if the option button is pressed or 0 if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, String option2, String option3, int messageType)
      Static helper method to easily display an three-option dialog. The dialog will remain until the user presses the Option1, Option2, Option3 or Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used.
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      option3 - The text to place on the third option button.
      messageType - The type of message to display
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option and so on. 0 is returned if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, String option2)
      Static helper method to easily display an two-option dialog. The dialog will remain until the user presses the Option1, Option2 or Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.\
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, String option2, int messageType)
      Static helper method to easily display an two-option dialog. The dialog will remain until the user presses the Option1, Option2 or Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled.
    • showOptionDialog

      public static int showOptionDialog(Component parent, String title, String message, String option1, String option2, Icon icon)
      Static helper method to easily display an two-option dialog. The dialog will remain until the user presses the Option1, Option2 or Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled.
    • showOptionNoCancelDialog

      public static int showOptionNoCancelDialog(Component parent, String title, String message, String option1, String option2, int messageType)
      Static helper method to easily display an two-option dialog. The dialog will remain until the user presses the Option1, Option2 or Cancel button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled.
    • showOptionNoCancelDialog

      public static int showOptionNoCancelDialog(Component parent, String title, String message, String option1, String option2, Icon icon)
      Static helper method to easily display an two-option dialog with no Cancel button. The dialog will remain until the user presses the Option1 or Option 2 button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      icon - allows the user to specify the icon to be used. If non-null, this will override the messageType.
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled.
    • showOptionNoCancelDialog

      public static int showOptionNoCancelDialog(Component parent, String title, String message, String option1, String option2, String option3, int messageType)
      Static helper method to easily display an three-option dialog with no Cancel button. The dialog will remain until the user presses the Option1, Option 2, or Option 3 button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      option1 - The text to place on the first option button.
      option2 - The text to place on the second option button.
      option3 - The text to place on the third option button.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled.
    • showOptionNoCancelDialog

      public static int showOptionNoCancelDialog(Component parent, String title, String message, String option1, String option2, String option3, int messageType, HelpLocation help)
      Static helper method to easily display an three-option dialog with no Cancel button. The dialog will remain until the user presses the Option1, Option 2, or Option 3 button.
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area
      message - The information message to be displayed in the dialog
      option1 - The text to place on the first option button
      option2 - The text to place on the second option button
      option3 - The text to place on the third option button
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      help - The help location for this dialog
      Returns:
      The options selected by the user. 1 for the first option and 2 for the second option. 0 is returned if the operation is cancelled
    • showYesNoDialog

      public static int showYesNoDialog(Component parent, String title, String message)
      Dialog with only YES/NO options, no CANCEL
      Parameters:
      parent - The parent dialog or frame of this dialog. (Can be null)
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      Returns:
      The options selected by the user:
                        0 is returned if the operation is cancelled
                        1 for Yes
                        2 for No
       
    • showYesNoDialogWithNoAsDefaultButton

      public static int showYesNoDialogWithNoAsDefaultButton(Component parent, String title, String message)
      Dialog with only YES/NO options, no CANCEL

      The dialog shown by this method will have the No button set as the default button so that an Enter key press will trigger a No action.

      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      Returns:
      The options selected by the user:
                        1 for Yes
                        2 for No
       
    • showYesNoCancelDialog

      public static int showYesNoCancelDialog(Component parent, String title, String message)
      Dialog with only YES/NO options, no CANCEL
      Parameters:
      parent - The parent component of this dialog. If the given component is a frame or dialog, then the component will be used to parent the option dialog. Otherwise, the parent frame or dialog will be found by traversing up the given component's parent hierarchy. Also, null can be used to not parent the dialog at all, but this promotes poor dialog behavior
      title - The String to be placed in the dialogs title area.
      message - The information message to be displayed in the dialog.
      Returns:
      The options selected by the user:
                        0 is returned if the operation is cancelled
                        1 for the first option
                        2 for the second option
       
    • showInputSingleLineDialog

      public static String showInputSingleLineDialog(Component parent, String title, String label, String initialValue)
      Displays a dialog for the user to enter a string value on a single line.
      Parameters:
      parent - the component to parent this dialog to
      title - the title to display on the input dialog
      label - the label to display in front of the text field
      initialValue - an optional value to set in the text field, can be null
      Returns:
      the string entered OR null if the dialog was canceled.
    • showInputMultilineDialog

      public static String showInputMultilineDialog(Component parent, String title, String label, String initialValue)
      Displays a dialog for the user to enter a multi-line string value.
      Parameters:
      parent - the component to parent this dialog to
      title - the title to display on the input dialog
      label - the label to display in front of the text area
      initialValue - an optional value that will be set in the text area, can be null
      Returns:
      the string entered OR null if the dialog was canceled.
    • showInputChoiceDialog

      public static String showInputChoiceDialog(Component parent, String title, String label, String[] selectableValues, String initialValue, int messageType)
      Displays a dialog for the user to enter a string value by either typing it or selecting from a list of possible strings.
      Parameters:
      parent - the component to parent this dialog to
      title - the title to display on the input dialog
      label - the label to display in front of the combo box
      selectableValues - an array of string to choose from
      initialValue - an optional value to set the combo box to, can be null in which the combo box will have the first item from the selectable values.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      the string entered or chosen OR null if the dialog was canceled.
    • showEditableInputChoiceDialog

      public static String showEditableInputChoiceDialog(Component parent, String title, String label, String[] selectableValues, String initialValue, int messageType)
      Displays a dialog for the user to enter a string value by either typing it or selecting from a list of possible strings. The list of possible values is editable such that the user can enter their own value by typing text.
      Parameters:
      parent - the component to parent this dialog to
      title - the title to display on the input dialog
      label - the label to display in front of the combo box
      selectableValues - an array of string to choose from
      initialValue - an optional value to set the combo box to, can be null in which the combo box will have the first item from the selectable values.
      messageType - used to specify a default icon, can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE)
      Returns:
      the string entered or chosen OR null if the dialog was canceled.
    • getResult

      public final int getResult()
      Returns which option was selected: CANCEL_OPTION if the operation was cancelled; OPTION_ONE if Option 1 was selected; OPTION_TWO if Option 2 was selected.
      Returns:
      selected option; returns CANCEL_OPTION for informational dialogs
    • okCallback

      protected void okCallback()
      callback for when the "OK" button is pressed.
      Overrides:
      okCallback in class DialogComponentProvider
    • cancelCallback

      protected void cancelCallback()
      Callback for when the cancel button is pressed.
      Overrides:
      cancelCallback in class DialogComponentProvider
    • getIconForMessageType

      public static Icon getIconForMessageType(int messageType)
      Returns the Icon to use for the given message type.
      Parameters:
      messageType - the type of message being displayed.
      Returns:
      the appropriate Icon.