Follow the Windows or macOS theme in a Swing app
Read the Windows or macOS theme at startup, fall back to the light palette if the lookup fails, and override the theme with a JVM launch option.
My Swing restaurant app reads the Windows or macOS appearance setting before opening its first window in
SYSTEMmode. A JVM option can pin either palette, and a failed lookup falls back to light.
The examples come from the Java 17 version of the Restaurant Information System (RSIS).
Keep the requested mode and the palette separate
Once I had two Swing palettes, I had to decide which one to load for the first window. At launch, I can pick light or dark, or ask the app to follow the desktop. By the time Swing creates a panel, that choice has to resolve to a palette the panel can paint with.
ThemeMode stores the launch preference, and ResolvedTheme holds the palette choice. With the lookup in startup code, each panel receives an AppTheme that already has the colors it needs. Pinning LIGHT or DARK also lets me rerun the app on another machine with the same palette.
| JVM option | System lookup | Palette at launch |
|---|---|---|
-Drsis.theme=LIGHT | Skipped | Light |
-Drsis.theme=DARK | Skipped | Dark |
-Drsis.theme=SYSTEM or omitted | Once at startup | Detected appearance |
| System mode with a failed or unavailable lookup | Failed or unsupported | Light |
RSIS reads rsis.theme as a JVM system property. The parser trims the value and converts it to uppercase, so dark works too. An empty or unrecognized value selects SYSTEM. I pin the theme for screenshots and use SYSTEM when I want to see what happens on a particular desktop. RSIS theme mode
Read the current Windows user’s app preference
On Windows, RSIS asks the registry for AppsUseLightTheme under the current user’s personalization settings. The command uses /v to return one named value. You can run the same query in PowerShell to see what the detector receives. Microsoft’s reg query reference
1
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" /v AppsUseLightTheme
The output can include the full registry path, blank lines, and an indented value line. The parser finds the line with AppsUseLightTheme and reads its last field. In AppsUseLightTheme REG_DWORD 0x0, the 0x0 means dark. A 0x1 means light. The parser also returns light when it can’t find the value name.
JUnit tests the parser by passing it strings for dark, light, and a missing value. One fixture includes the registry path and blank lines. Another ends in Windows-style CRLF. You can run those cases on any development machine. On Windows, you can also compare the live reg query output with what the app logs at startup. RSIS detector and Windows parser
Use the global appearance preference on macOS
On macOS, RSIS runs defaults read -g under the logged-in account. The -g flag reads the user’s global preferences, which apps can share. Apple’s preferences domain documentation
1
defaults read -g AppleInterfaceStyle
RSIS trims the output and compares it with Dark, ignoring case. A match selects dark. Other output, including an error message for a missing key, selects light. Both platforms return ResolvedTheme, so the Windows and macOS output formats stay in the detector. Another desktop implementation would need to read its own appearance setting and return the same type.
RSIS chooses the palette once at startup, so relaunch it after changing the desktop setting. To switch themes while an app is open, you’d also need to listen for preference changes and update the components already on screen.
Resolve the palette before entering the EDT
Startup reads the JVM property first and runs the detector only for SYSTEM. It then builds the AppTheme with the palette and typography RSIS loaded from its bundled fonts.
1
2
3
4
5
ThemeMode mode = ThemeMode.parse(System.getProperty("rsis.theme"));
ResolvedTheme resolved = mode.resolve(mode == ThemeMode.SYSTEM
? new WindowsMacOsThemeDetector().detect()
: ResolvedTheme.LIGHT);
AppTheme theme = AppTheme.of(resolved, typography);
The startup thread launches the OS command with ProcessBuilder and waits up to three seconds for it to exit. A timeout or I/O failure selects light. If something interrupts the wait, the detector restores the thread’s interrupt flag. Once the palette is ready, RSIS calls SwingUtilities.invokeLater. On the event dispatch thread, it installs the theme defaults and builds the screens. RSIS startup sequence
Figure 1. On the Linux runner, SYSTEM opens the app in the default light theme.
Figure 2. The same app starts in dark mode on Linux with an explicit DARK setting.
Verify the lookup and the override
On Windows or macOS, set the desktop to dark and run each Java command from an RSIS checkout. Close the app between runs. With a working system lookup, SYSTEM should open in dark and LIGHT should open in light. Compare the same view in both runs. The startup log records the requested mode and the palette that was applied.
1
2
3
4
./gradlew shadowJar
java -Drsis.theme=SYSTEM -jar build/libs/restaurant-information-system-2.0.0-SNAPSHOT-all.jar
java -Drsis.theme=LIGHT -jar build/libs/restaurant-information-system-2.0.0-SNAPSHOT-all.jar
./gradlew test --tests 'org.example.rsis.infrastructure.system.WindowsMacOsThemeDetectorTest'
In Windows PowerShell, use .\gradlew.bat for Gradle and quote JVM options like "-Drsis.theme=SYSTEM". Run the parser test on any development machine to check the Windows output cases without changing the desktop theme.
- Launch in
SYSTEMon a dark desktop and check the resolved palette. - Force
LIGHTon the same desktop and compare the same view. - Keep a light fallback for missing settings, failed commands, and unhandled platforms.