Class SwingGUI

java.lang.Object
com.everdro1d.libs.swing.SwingGUI

public class SwingGUI extends Object
The SwingGUI class provides utility methods for setting up the look and feel of a Swing application, managing dark mode, and handling various GUI components.

This class is non-instantiable and contains only static methods.

Features:

  • Setup look and feel with optional dark mode support.
  • Get all frames in the current application.
  • Simulate key and mouse events on components.
  • Create and update combo boxes with custom font settings.
  • Set hand cursor for clickable components.

Example Usage:

 SwingGUI.setupLookAndFeel(true, true, false);
 

Note: This class cannot be instantiated.

  • Method Details

    • setupLookAndFeel

      public static void setupLookAndFeel(boolean useFlatLaf, boolean allowDarkMode, boolean startInDarkMode)
      Set the look and feel of the application.
      Parameters:
      useFlatLaf - whether to use FlatLaf (defaults to system look and feel)
      allowDarkMode - whether to enable dark mode (FlatLaf only)
      startInDarkMode - whether to start the application in dark mode (FlatLaf only)
    • getAllFrames

      public static JFrame[] getAllFrames()
      Get all frames in the current application.

      NOTE: Use SwingUtilities.invokeLater(() -> {} when calling this method.

      Returns:
      array of JFrames
    • switchLightOrDarkMode

      public static void switchLightOrDarkMode(boolean isDarkModeEnabled)
      Used to enable dark mode for the running application. Attempts to get all frames when called.

      FlatLaf is used to set the look and feel of the application

      Parameters:
      isDarkModeEnabled - whether to enable dark mode
      See Also:
    • switchLightOrDarkMode

      public static void switchLightOrDarkMode(boolean isDarkModeEnabled, JFrame[] frames)
      Used to enable dark mode for the running application.

      FlatLaf is used to set the look and feel of the application

      Parameters:
      isDarkModeEnabled - whether to enable dark mode
      frames - array of JFrames to update
      See Also:
    • uiSetup

      public static void uiSetup(Font font)
      Set the default UI settings for the application.
      Parameters:
      font - default font for the application (plain)
    • uiSetup

      public static void uiSetup(String fontName, int fontSize)
      Set the default UI settings for the application.
      Parameters:
      fontName - the name of the font to use
      fontSize - the size of the font to use
    • getFramePositionOnScreen

      public static int[] getFramePositionOnScreen(JFrame frame)
      Gets the window's position on the current monitor.
      Parameters:
      frame - the frame to get the position of
      Returns:
      int[] = {x, y, activeMonitor}
    • setFramePosition

      public static void setFramePosition(JFrame frame, int framePosX, int framePosY, int activeMonitor)
      Set the position of the frame on the screen.
      Parameters:
      frame - the frame to set the position of
      framePosX - the x position of the frame
      framePosY - the y position of the frame
      activeMonitor - the active monitor
    • setLocationOnResize

      public static void setLocationOnResize(JFrame frame, boolean keepOnActiveMonitor)
      Set the location of the frame on the screen when it is resized.
      Parameters:
      frame - the frame to set the location of
      keepOnActiveMonitor - whether to adjust the frame to fit fully on the active monitor
    • getWindowSize

      public static void getWindowSize(JFrame frame)
      Get the size of the window. Used for debugging.
      Parameters:
      frame - the frame to get the size of
    • requestFocusAndSimulateKeyEvent

      public static void requestFocusAndSimulateKeyEvent(JComponent component)
      Simulate a key event after requesting focus on a component.
      Parameters:
      component - the component to request focus on
      See Also:
    • simulateKeyEvent

      public static void simulateKeyEvent(JComponent component)
      Simulates an ENTER keyReleased event on a component.
      Parameters:
      component - the component to simulate the event on
      See Also:
    • simulateKeyEvent

      public static void simulateKeyEvent(JComponent component, int keyCode, char keyChar, int modifiers, int event)
      Simulates a keyEvent event on a component.
      Parameters:
      component - the component to simulate the event on
      keyCode - ex: KeyEvent.VK_ENTER for ENTER
      keyChar - ex: '\n' for ENTER
      modifiers - One of: { Event.SHIFT_MASK,Event.CTRL_MASK, Event.META_MASK,Event.ALT_MASK, InputEvent.SHIFT_DOWN_MASK, InputEvent.CTRL_DOWN_MASK, InputEvent.META_DOWN_MASK, InputEvent.ALT_DOWN_MASK, InputEvent.BUTTON1_DOWN_MASK, InputEvent.BUTTON2_DOWN_MASK, InputEvent.BUTTON3_DOWN_MASK, InputEvent.ALT_GRAPH_DOWN_MASK }
      event - KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED, or KeyEvent.KEY_TYPED
      See Also:
    • simulateMouseEvent

      public static void simulateMouseEvent(JComponent component, int x, int y, int event)
      Simulates a mouse event on a component.
      Parameters:
      component - the component to simulate the event on
      x - the x position of the mouse
      y - the y position of the mouse
      event - MouseEvent.MOUSE_[PRESSED, RELEASED, CLICKED, ENTERED, EXITED]
    • stringComboBoxConstructor

      public static JComboBox<String> stringComboBoxConstructor(String[] strArr, int selectedIndex, String fontName, int fontSize)
      Creates a string combo box with the specified parameters.
      Parameters:
      strArr - the array of items for the combo box
      selectedIndex - default selected index
      fontName - the name of the font to use
      fontSize - the size of the font to use
      Returns:
      the combo box
    • setupComboBox

      public static void setupComboBox(JComboBox<String> comboBox, int selectedIndex, String fontName, int fontSize)
      Set up the combo box with the specified parameters.
      Parameters:
      comboBox - the combo box to set up
      selectedIndex - default selected index
      fontName - the name of the font to use
      fontSize - the size of the font to use
      See Also:
    • updateComboBox

      public static void updateComboBox(String[] strArr, JComboBox<String> comboBox)
      Update the width of the combobox depending on the width of the longest string within its array. Padding defaults to 40.
      Parameters:
      strArr - the array of items for the combo box
      comboBox - the combo box to update
      See Also:
    • updateComboBox

      public static void updateComboBox(String[] strArr, JComboBox<String> comboBox, int padding)
      Update the width of the combobox depending on the width of the longest string within its array.
      Parameters:
      strArr - the array of items for the combo box
      comboBox - the combo box to update
      padding - the padding to add to the width
    • setHandCursorToClickableComponents

      public static void setHandCursorToClickableComponents(Container container)
      Set the cursor to the hand cursor for clickable components.
      Parameters:
      container - the container of the components (top level JFrame etc.)
    • setProgressPercent

      public static void setProgressPercent(int i, JProgressBar progressBar)
      Sets the progress bar in percentage format "1-100%", while ensuring non-jumpy values (only increases not decreases).
      Parameters:
      i - current progress percentage
      progressBar - progressBar to change

      Example Use Case:

      Download ~X mb out of 100 mb where the estimated progress fluctuates.
    • isDarkModeActive

      public static boolean isDarkModeActive()
      Check if the current look and feel is dark mode.
      Returns:
      true if dark mode is active, false otherwise
    • canHoldText

      public static boolean canHoldText(JComponent component)
      Check if the component can hold text.
      Parameters:
      component - the component to check
      Returns:
      true if the component can hold text, false otherwise
    • updateFrameColors

      public static void updateFrameColors(JFrame frame, boolean isDarkModeEnabled)
      Update the colors of the frame and its components.
      Parameters:
      frame - the frame to update
      isDarkModeEnabled - whether dark mode is enabled
    • getAllComponents

      public static ArrayList<JComponent> getAllComponents(JFrame frame)
      Get all components in a JFrame.
      Parameters:
      frame - the frame to get the components from
      Returns:
      an ArrayList of JComponents
    • getAllComponents

      public static ArrayList<JComponent> getAllComponents(Container container)
      Get all components in a container.
      Parameters:
      container - the container to get the components from
      Returns:
      an ArrayList of JComponents
    • getDefaultFontNameForOS

      public static String getDefaultFontNameForOS()
      Get the default font name for the current operating system.

      This method first checks if the "Tahoma" font is available on the system. If it is, it returns "Tahoma". Otherwise, it falls back to the default font for the current operating system:

      • Windows: "Segoe UI"
      • Mac: "San Francisco"
      • Unix: "Liberation Sans"
      • Everything Else: "Arial"

      Note that if none of these fonts are available, the font will be up to the look and feel to decide.

      Returns:
      the default font name