Skip to content
Advertisement

user’s preferred contrast polarity? (OS dark mode setting)

Current operating systems (e.g. Windows, macOS) let the user decide if they want dark content on light blackground (classic) or rather light content on dark blackground (dark mode).

Is there something available in Java, so that my (client side) Java programs can automatically adjust their contrast polarity according to that OS user setting? I am using Swing, if that matters.

I found the system property apple.awt.application.appearance, but nothing for Windows and nothing operating system independent.

And also, is there a listener? Because that setting might change on runtime.

Motivation: inverse contrast polarity seems to inhibit myopia (Aleman et al. 2018-07-18 Science)

Advertisement

Answer

There is a feature request (JDK-8235460) submitted in 2019.

In the meantime you might want to try jSystemThemeDetector (Apache-2 license). This library works on Windows, macOS and some Linux distributions.

OsThemeDetector.getDetector().isDark()

You can also add a listener to detect changes.

OsThemeDetector.getDetector().registerListener(isDark -> {
    SwingUtilities.invokeLater(() -> {
        if (isDark) {
            // The OS switched to a dark theme
        } else {
            // The OS switched to a light theme
        }
    });
}); 

Note: For JavaFX you use Platform.runLater instead of SwingUtilities.invokeLater.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement