In most modern operating systems there is a dark mode option. This library is created for detecting this using java.
It can be useful for example if you want to synchronize your GUI App's look and feel with the operating system.
This library is inspired by the dark-theme detection in Intellij Idea.
It works on Windows 10, MacOS Mojave (or later) and even on some Linux distributions.
Java 11 or higher
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
}
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
}
});
If you use the listener for changing the UI in a JavaFX application, make sure you use Platform.runLater
in it:
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
}
});
});
It's important because if you are doing JavaFX specific stuff in the listener, you should execute it on the UI thread (JavaFX Application Thread
).
Otherwise, you might face some serious issues.
In case of AWT/Swing, use SwingUtilities.invokeLater
for the same reason:
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
}
});
});
It's available on JitPack!
Gradle example:
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.Dansoftowner:jSystemThemeDetector:3.6'
}
There is a Demo application available here. It's a basic JavaFX application that changes the UI when the OS switched to a dark/light theme.
- Boomega - A modern book explorer & catalog application
- Document Archiver - Archive all your documents in a consistent way, which enables you to retrieve them later fast and easy.
- pearletj - Cross-platform multi-chain thick wallet for Cryto-currencies
If this library is used by your project, feel free to create a PR and mention that in this section!
- SLF4J - Simple Logging Facade for Java
- Jetbrains Annotations - Annotations for JVM-based languages
- JNA - Java Native Access
- JFA - Java Foundation Access
- OSHI - Operating system & hardware information
- Version Compare - Lightweight Android & Java library to compare version strings.