Class Popups

java.lang.Object
nl.colorize.util.swing.Popups

public final class Popups extends Object
Utility class for working with pop-up modal dialog windows in Swing applications.

There are two ways to create pop-up windows: By using builder() and its methods to configure and show the pop-up window, or by using one of the "pre-baked" static methods that create a pop-up dialog in one go.

Both approaches wrap the underlying JOptionPane. It makes sure that pop-up windows look native on each platform, automatically word-wraps long messages, and provides a number of convenience methods.

It's recommended to not rely on generic button labels such as "OK" or "cancel", since they can be confusing depending on the action performed by the pop-up window. Instead, try to use button labels that convey the action that is going to be performed, such as "save" or "download file".

  • Field Details

  • Method Details

    • withTitle

      public Popups withTitle(String title)
    • withButtons

      public Popups withButtons(List<String> buttonLabels)
    • withButtons

      public Popups withButtons(String... buttonLabels)
    • withConfirmButtons

      public Popups withConfirmButtons()
    • withPanel

      public Popups withPanel(JComponent panel)
    • withMessage

      public Popups withMessage(String message)
    • withWarningIcon

      public Popups withWarningIcon()
    • withErrorIcon

      public Popups withErrorIcon()
    • show

      public int show(JFrame parentWindow)
      Displays a pop-up window based on the configuration in this builder. The pop-up window will be modal for the specified parent window.
      Returns:
      The index of the button that was clicked. Returns -1 if the pop-up window was disposed of without explicitly clicking one of its buttons.
    • show

      public int show()
      Displays a pop-up window based on the configuration in this builder. The pop-up window will be modal for the entire application.
      Returns:
      The index of the button that was clicked. Returns -1 if the pop-up window was disposed of without explicitly clicking one of its buttons.
    • builder

      public static Popups builder()
      Starts building a new pop-up window. The initial state will have no title, an empty message, and a single default "OK" button.
    • message

      public static int message(JFrame parent, String title, JComponent panel, List<String> buttons)
      Shows a pop-up window that consists of the specified component and a number of buttons. The first button in the list is considered the "primary" button. This method will block until either one of the buttons is clicked or the dialog was cancelled.
      Returns:
      The index of the button that was clicked. For example, if the first button from buttons was clicked this will return 0.
      Throws:
      IllegalArgumentException - if no buttons were supplied.
    • message

      public static int message(JFrame parent, String title, JComponent panel, String... buttons)
      Shows a pop-up window that consists of the specified component and a number of buttons. The first button in the list is considered the "primary" button. This method will block until either one of the buttons is clicked or the dialog was cancelled.
      Returns:
      The index of the button that was clicked. For example, if the first button from buttons was clicked this will return 0.
      Throws:
      IllegalArgumentException - if no buttons were supplied.
    • message

      public static int message(JFrame parent, String title, String message, List<String> buttons)
      Shows a pop-up window that consists of the specified component and a number of buttons. The first button in the list is considered the "primary" button. This method will block until either one of the buttons is clicked or the dialog was cancelled.
      Returns:
      The index of the button that was clicked. For example, if the first button from buttons was clicked this will return 0.
      Throws:
      IllegalArgumentException - if no buttons were supplied.
    • message

      public static int message(JFrame parent, String title, String message, String... buttons)
      Shows a pop-up window that consists of the specified component and a number of buttons. The first button in the list is considered the "primary" button. This method will block until either one of the buttons is clicked or the dialog was cancelled.
      Returns:
      The index of the button that was clicked. For example, if the first button from buttons was clicked this will return 0.
      Throws:
      IllegalArgumentException - if no buttons were supplied.
    • message

      public static void message(JFrame parent, String title, JComponent message)
      Shows a simple pop-up window that displays a component and contains a default "OK" button.
    • message

      public static void message(JFrame parent, String title, String message)
      Shows a simple pop-up window that displays a text message and contains a default "OK" button.
    • message

      public static void message(JFrame parent, String message)
      Shows a simple pop-up window that displays a text message and contains a default "OK" button.
    • errorMessage

      public static void errorMessage(JFrame parent, String message)
      Shows a simple pop-up window that displays an error message with a generic "Error" title and a single "OK" button.