Class MessageUtil

java.lang.Object
ca.corbett.extras.MessageUtil

public final class MessageUtil extends Object
Simple utility class to show messages to the user via JOptionPane while also simultaneously logging them. This saves the caller from having to make two separate calls each time an error comes up.

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.
Since:
2022-04-11
Author:
scorbo2
  • Constructor Summary

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    void
    error(String message)
    Shows an error message to the user, and also logs it if a Logger was provided.
    void
    error(String title, String message)
    Shows an error message to the user, and also logs it if a Logger was provided.
    void
    error(String title, String message, Throwable ex)
    Shows an error message to the user, and also logs it if a Logger was provided.
    void
    error(String message, Throwable ex)
    Shows an error message to the user, and also logs it if a Logger was provided.
    Returns the Logger currently being used.
    void
    info(String message)
    Shows an info message to the user, and also logs it if a Logger was provided.
    void
    info(String title, String message)
    Shows an info message to the user, and also logs it if a Logger was provided.
    void
    Used to specify a default dialog title in the case where error() is invoked without a given title.
    void
    Used to specify a default dialog title in the case where info() is invoked without a given title.
    void
    warning(String message)
    Shows a warning message to the user, and also logs it if a Logger was provided.
    void
    warning(String title, String message)
    Shows a warning message to the user, and also logs it if a Logger was provided.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MessageUtil

      public MessageUtil(Component parent)
      Creates a MessageUtil with the given parent Component.
      Parameters:
      parent - The parent Component to be used with message dialogs.
    • MessageUtil

      public MessageUtil(Component parent, Logger logger)
      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

    • getLogger

      public Logger getLogger()
      Returns the Logger currently being used. May be null if none was set.
      Returns:
      A Logger instance, or null.
    • setDefaultErrorTitle

      public void setDefaultErrorTitle(String title)
      Used to specify a default dialog title in the case where error() is invoked without a given title. Does not effect logging. If unset, the default is "Error".
      Parameters:
      title - The new title to use for error dialogs.
    • setDefaultInfoTitle

      public void setDefaultInfoTitle(String title)
      Used to specify a default dialog title in the case where info() is invoked without a given title. Does not effect logging. If unset, the default is "Info".
      Parameters:
      title - The new title to use for info dialogs.
    • error

      public void error(String message)
      Shows an error message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The error message to show.
    • error

      public void error(String title, String message)
      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

      public void error(String message, Throwable ex)
      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

      public void error(String title, String message, Throwable ex)
      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

      public void info(String message)
      Shows an info message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The info message to show.
    • info

      public void info(String title, String message)
      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

      public void warning(String message)
      Shows a warning message to the user, and also logs it if a Logger was provided.
      Parameters:
      message - The warning message to show.
    • warning

      public void warning(String title, String message)
      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.