Compatibility and Requirements
masterRequirements
- Java 11 or higher
Supported Operating Systems
- Windows 10
- MacOS Mojave (or later)
- Linux (tested on Ubuntu with Gnome and KDE Plasma)
repository·master·Indexed 42 days ago
https://github.com/dansoftowner/jsystemthemedetectorA Java library for detecting whether the host operating system is using a dark or light theme. It supports Windows 10, MacOS Mojave+, and Linux, allowing Java-based GUI applications like Swing and JavaFX to synchronize with system preferences via OsThemeDetector.
To use jSystemThemeDetector in a Gradle project, add the JitPack repository to your repositories block and include the dependency in your dependencies block. Note that the version used in the example is 3.6; ensure you check JitPack for the latest available version.
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.Dansoftowner:jSystemThemeDetector:3.6'
}When updating a GUI in response to a theme change, you must ensure the UI updates occur on the correct application thread to avoid stability issues.
Platform.runLater() to execute it on the JavaFX Application Thread.SwingUtilities.invokeLater() to execute it on the Event Dispatch Thread.// JavaFX Example
final OsThemeDetector detector = OsThemeDetector.getDetector();
detector.registerListener(isDark -> {
Platform.runLater(() -> {
if (isDark) {
// The OS switched to a dark theme
} else {
// The OS switched to a light theme
}
});
});
// Swing Example
final OsThemeDetector detector = OsThemeDetector.getDetector();
detector.registerListener(isDark -> {
SwingUtilities.invokeLater(() -> {
if (isDark) {
// The OS switched to a dark theme
} else {
// The OS switched to a light theme
}
});
});Use OsThemeDetector.getDetector() to obtain an instance of the detector. You can then call .isDark() to check if the operating system is currently using a dark theme.
final OsThemeDetector detector = OsThemeDetector.getDetector();
final boolean isDarkThemeUsed = detector.isDark();
if (isDarkThemeUsed) {
//The OS uses a dark theme
} else {
//The OS uses a light theme
}You can register a listener using registerListener to react whenever the user switches between light and dark modes in their operating system settings. The listener receives a boolean indicating whether the new theme is dark.
final OsThemeDetector detector = OsThemeDetector.getDetector();
detector.registerListener(isDark -> {
if (isDark) {
//The OS switched to a dark theme
} else {
//The OS switched to a light theme
}
});