Class MessageUtil
General logging and messaging
Example usage:
MessageUtil messageUtil = new MessageUtil(this, myLogger);
messageUtil.error("Something went horribly wrong", someException);
This will show an error dialog to the user, and also log the message string to the
given Logger. It is equivalent to the following code:
JOptionPane.showMessageDialog(this, "Something went horribly wrong",
"Error", JOptionPane.ERROR_MESSAGE);
myLogger.log(Level.SEVERE, "Something went horribly wrong", someException);
Both the given parent component and the given Logger are optional. If no parent component is given, dialogs will have no parent. If no Logger is given, no logging will occur.
Asking questions
MessageUtil has shorthand methods for asking simple yes/no or yes/no/cancel questions via JOptionPane. These methods do not perform any logging. For example:
int result = messageUtil.askYesNo("Confirm Delete",
"Are you sure you want to delete this file?");
if (result == MessageUtil.YES) {
// User clicked Yes
// ...
}
This saves a small amount of code and improves readability.
Getting user input
MessageUtil also has shorthand methods for asking the user to select from a list of options, or to enter free-form text input. These, once again, are wrappers around JOptionPane methods that save a small amount of code and improve readability. Examples:
// Get free-text entry with some default value already filled in:
String color = messageUtil.askText("Select Color",
"Enter your favorite color:",
"Blue");
if (color != null) {
// User made a selection (null would mean they cancelled)
}
// Get selection from a list of options:
String[] options = {"Red", "Green", "Blue"};
String color = messageUtil.askSelect("Select Color",
"Choose your favorite color:",
options,
"Green");
if (color != null) {
// User made a selection (null would mean they cancelled)
}
Nothing is logged when asking for user input.
- Since:
- 2022-04-11
- Author:
- scorbo2
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMessageUtil(Component parent) Creates a MessageUtil with the given parent Component.MessageUtil(Component parent, Logger logger) Creates a MessageUtil with the given parent Component and the given Logger. -
Method Summary
Modifier and TypeMethodDescriptionShorthand for using JOptionPane to ask the user to select from a list of options.Shorthand for using JOptionPane to ask the user to select from a list of options.Shorthand for using JOptionPane to ask the user for free-form text input.Shorthand for using JOptionPane to ask the user for free-form text input.intShorthand for using JOptionPane to ask the user a simple yes or no question.intShorthand for using JOptionPane to ask the user a simple yes or no question.intaskYesNoCancel(String message) Shorthand for using JOptionPane to ask the user a yes, no, or cancel question.intaskYesNoCancel(String title, String message) Shorthand for using JOptionPane to ask the user a yes, no, or cancel question.voidShows an error message to the user, and also logs it if a Logger was provided.voidShows an error message to the user, and also logs it if a Logger was provided.voidShows an error message to the user, and also logs it if a Logger was provided.voidShows an error message to the user, and also logs it if a Logger was provided.Returns the Logger currently being used.Returns the configured parent Component, if one is set.voidShows an info message to the user, and also logs it if a Logger was provided.voidShows an info message to the user, and also logs it if a Logger was provided.voidsetDefaultErrorTitle(String title) Used to specify a default dialog title in the case where error() is invoked without a given title.voidsetDefaultInfoTitle(String title) Used to specify a default dialog title in the case where info() is invoked without a given title.voidsetDefaultInputTitle(String title) Used to specify a default dialog title in the case where input dialogs are invoked without a given title.voidsetDefaultQuestionTitle(String title) Used to specify a default dialog title in the case where askYesNo() or askYesNoCancel() is invoked without a given title.voidsetDefaultWarningTitle(String title) Used to specify a default dialog title in the case where warning() is invoked without a given title.voidShows a warning message to the user, and also logs it if a Logger was provided.voidShows a warning message to the user, and also logs it if a Logger was provided.
-
Field Details
-
YES
public static final int YES- See Also:
-
NO
public static final int NO- See Also:
-
CANCEL
public static final int CANCEL- See Also:
-
-
Constructor Details
-
MessageUtil
Creates a MessageUtil with the given parent Component.- Parameters:
parent- The parent Component to be used with message dialogs.
-
MessageUtil
Creates a MessageUtil with the given parent Component and the given Logger.- Parameters:
parent- The parent Component to be used with message dialogs.logger- The Logger which will receive INFO and SEVERE messages.
-
-
Method Details
-
getParent
Returns the configured parent Component, if one is set. May be null.- Returns:
- The parent Component, or null.
-
getLogger
Returns the Logger currently being used. May be null if none was set.- Returns:
- A Logger instance, or null.
-
setDefaultErrorTitle
Used to specify a default dialog title in the case where error() is invoked without a given title. Does not affect logging. If unset, the default is "Error".- Parameters:
title- The new title to use for error dialogs.
-
setDefaultWarningTitle
Used to specify a default dialog title in the case where warning() is invoked without a given title. Does not affect logging. If unset, the default is "Warning".- Parameters:
title- The new title to use for warning dialogs.
-
setDefaultInfoTitle
Used to specify a default dialog title in the case where info() is invoked without a given title. Does not affect logging. If unset, the default is "Info".- Parameters:
title- The new title to use for info dialogs.
-
setDefaultQuestionTitle
Used to specify a default dialog title in the case where askYesNo() or askYesNoCancel() is invoked without a given title. If unset, the default is "Confirm".- Parameters:
title- The new title to use for question dialogs.
-
setDefaultInputTitle
Used to specify a default dialog title in the case where input dialogs are invoked without a given title. If unset, the default is "Select".- Parameters:
title- The new title to use for input dialogs.
-
error
Shows an error message to the user, and also logs it if a Logger was provided.- Parameters:
message- The error message to show.
-
error
Shows an error message to the user, and also logs it if a Logger was provided.- Parameters:
title- The title for the dialog.message- The error message to show.
-
error
Shows an error message to the user, and also logs it if a Logger was provided.- Parameters:
message- The error message to show.ex- An Exception which will be handed to the Logger.
-
error
Shows an error message to the user, and also logs it if a Logger was provided.- Parameters:
title- The title for the dialog.message- The error message to show.ex- An Exception which will be handed to the Logger.
-
info
Shows an info message to the user, and also logs it if a Logger was provided.- Parameters:
message- The info message to show.
-
info
Shows an info message to the user, and also logs it if a Logger was provided.- Parameters:
title- The title for the dialog.message- The info message to show.
-
warning
Shows a warning message to the user, and also logs it if a Logger was provided.- Parameters:
message- The warning message to show.
-
warning
Shows a warning message to the user, and also logs it if a Logger was provided.- Parameters:
title- The title for the dialog.message- The warning message to show.
-
askYesNo
Shorthand for using JOptionPane to ask the user a simple yes or no question. Nothing is logged. A default dialog title of "Confirm" is used.- Parameters:
message- The question message.- Returns:
- One of JOptionPane.YES_OPTION or JOptionPane.NO_OPTION (shorthand: MessageUtil.YES or MessageUtil.NO).
-
askYesNoCancel
Shorthand for using JOptionPane to ask the user a yes, no, or cancel question. Nothing is logged. A default dialog title of "Confirm" is used.- Parameters:
message- The question message.- Returns:
- One of JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION (shorthand: MessageUtil.YES, MessageUtil.NO, or MessageUtil.CANCEL).
-
askYesNo
Shorthand for using JOptionPane to ask the user a simple yes or no question. Nothing is logged.- Parameters:
title- The title for the dialog.message- The question message.- Returns:
- One of JOptionPane.YES_OPTION or JOptionPane.NO_OPTION (shorthand: MessageUtil.YES or MessageUtil.NO).
-
askYesNoCancel
Shorthand for using JOptionPane to ask the user a yes, no, or cancel question. Nothing is logged.- Parameters:
title- The title for the dialog.message- The question message.- Returns:
- One of JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION (shorthand: MessageUtil.YES, MessageUtil.NO, or MessageUtil.CANCEL).
-
askSelect
Shorthand for using JOptionPane to ask the user to select from a list of options. A default dialog title of "Select" is used. Nothing is logged.- Parameters:
message- The prompt message.options- An array of possible options in String form.initialSelectionValue- The option that should be initially selected when the dialog appears.- Returns:
- The selected option as a String, or null if the user cancelled.
-
askSelect
public String askSelect(String title, String message, String[] options, String initialSelectionValue) Shorthand for using JOptionPane to ask the user to select from a list of options. Nothing is logged.- Parameters:
title- The title for the dialog.message- The prompt message.options- An array of possible options in String form.initialSelectionValue- The option that should be initially selected when the dialog appears.- Returns:
- The selected option as a String, or null if the user cancelled.
-
askText
Shorthand for using JOptionPane to ask the user for free-form text input. A default dialog title of "Select" is used. Nothing is logged.- Parameters:
message- The prompt message.initialValue- The initial value to pre-fill in the input field.- Returns:
- The entered text as a String, or null if the user cancelled.
-
askText
Shorthand for using JOptionPane to ask the user for free-form text input. Nothing is logged.- Parameters:
title- The title for the dialog.message- The prompt message.initialValue- The initial value to pre-fill in the input field.- Returns:
- The entered text as a String, or null if the user cancelled.
-