Package com.everdro1d.libs.swing
Class SwingGUI
java.lang.Object
com.everdro1d.libs.swing.SwingGUI
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 Summary
Modifier and TypeMethodDescriptionstatic boolean
canHoldText
(JComponent component) Check if the component can hold text.static ArrayList
<JComponent> getAllComponents
(Container container) Get all components in a container.static ArrayList
<JComponent> getAllComponents
(JFrame frame) Get all components in a JFrame.static JFrame[]
Get all frames in the current application.static String
Get the default font name for the current operating system.static int[]
getFramePositionOnScreen
(JFrame frame) Gets the window's position on the current monitor.static void
getWindowSize
(JFrame frame) Get the size of the window.static boolean
Check if the current look and feel is dark mode.static void
requestFocusAndSimulateKeyEvent
(JComponent component) Simulate a key event after requesting focus on a component.static void
setFramePosition
(JFrame frame, int framePosX, int framePosY, int activeMonitor) Set the position of the frame on the screen.static void
setHandCursorToClickableComponents
(Container container) Set the cursor to the hand cursor for clickable components.static void
setLocationOnResize
(JFrame frame, boolean keepOnActiveMonitor) Set the location of the frame on the screen when it is resized.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).static void
setupComboBox
(JComboBox<String> comboBox, int selectedIndex, String fontName, int fontSize) Set up the combo box with the specified parameters.static void
setupLookAndFeel
(boolean useFlatLaf, boolean allowDarkMode, boolean startInDarkMode) Set the look and feel of the application.static void
simulateKeyEvent
(JComponent component) Simulates an ENTER keyReleased event on a component.static void
simulateKeyEvent
(JComponent component, int keyCode, char keyChar, int modifiers, int event) Simulates a keyEvent event on a component.static void
simulateMouseEvent
(JComponent component, int x, int y, int event) Simulates a mouse event on a component.stringComboBoxConstructor
(String[] strArr, int selectedIndex, String fontName, int fontSize) Creates a string combo box with the specified parameters.static void
switchLightOrDarkMode
(boolean isDarkModeEnabled) Used to enable dark mode for the running application.static void
switchLightOrDarkMode
(boolean isDarkModeEnabled, JFrame[] frames) Used to enable dark mode for the running application.static void
Set the default UI settings for the application.static void
Set the default UI settings for the application.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.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.static void
updateFrameColors
(JFrame frame, boolean isDarkModeEnabled) Update the colors of the frame and its components.
-
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
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
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 modeframes
- array of JFrames to update- See Also:
-
uiSetup
Set the default UI settings for the application.- Parameters:
font
- default font for the application (plain)
-
uiSetup
Set the default UI settings for the application.- Parameters:
fontName
- the name of the font to usefontSize
- the size of the font to use
-
getFramePositionOnScreen
Gets the window's position on the current monitor.- Parameters:
frame
- the frame to get the position of- Returns:
- int[] = {x, y, activeMonitor}
-
setFramePosition
Set the position of the frame on the screen.- Parameters:
frame
- the frame to set the position offramePosX
- the x position of the frameframePosY
- the y position of the frameactiveMonitor
- the active monitor
-
setLocationOnResize
Set the location of the frame on the screen when it is resized.- Parameters:
frame
- the frame to set the location ofkeepOnActiveMonitor
- whether to adjust the frame to fit fully on the active monitor
-
getWindowSize
Get the size of the window. Used for debugging.- Parameters:
frame
- the frame to get the size of
-
requestFocusAndSimulateKeyEvent
Simulate a key event after requesting focus on a component.- Parameters:
component
- the component to request focus on- See Also:
-
simulateKeyEvent
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 onkeyCode
- ex: KeyEvent.VK_ENTER for ENTERkeyChar
- ex: '\n' for ENTERmodifiers
- 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
Simulates a mouse event on a component.- Parameters:
component
- the component to simulate the event onx
- the x position of the mousey
- the y position of the mouseevent
- 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 boxselectedIndex
- default selected indexfontName
- the name of the font to usefontSize
- 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 upselectedIndex
- default selected indexfontName
- the name of the font to usefontSize
- the size of the font to use- See Also:
-
updateComboBox
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 boxcomboBox
- the combo box to update- See Also:
-
updateComboBox
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 boxcomboBox
- the combo box to updatepadding
- the padding to add to the width
-
setHandCursorToClickableComponents
Set the cursor to the hand cursor for clickable components.- Parameters:
container
- the container of the components (top level JFrame etc.)
-
setProgressPercent
Sets the progress bar in percentage format "1-100%", while ensuring non-jumpy values (only increases not decreases).- Parameters:
i
- current progress percentageprogressBar
- progressBar to changeExample 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
Check if the component can hold text.- Parameters:
component
- the component to check- Returns:
- true if the component can hold text, false otherwise
-
updateFrameColors
Update the colors of the frame and its components.- Parameters:
frame
- the frame to updateisDarkModeEnabled
- whether dark mode is enabled
-
getAllComponents
Get all components in a JFrame.- Parameters:
frame
- the frame to get the components from- Returns:
- an ArrayList of JComponents
-
getAllComponents
Get all components in a container.- Parameters:
container
- the container to get the components from- Returns:
- an ArrayList of JComponents
-
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
-