6

Currently I am developing an application, However I am unaware of any methods to check if the system is in dark mode already.The application is made with JavaFX on a Mac. Currently I have a CheckMenuItem which when checked, loads up with the use of an IF statement a 'darkmode.css' file. And removes it if the file if the checkmenuitem box is unchecked.

However instead of a check MenuItem I want to be able to activate the css file when the OS is in darkmode and when its not for it to remove it again. I feel like that Java may not be able to do this. If there is any ways for this to be done I will be keen on learning and interested on how you may have implemented it.

In a nutshell native dark mode almost.

2 Answers2

6

There is no standard Java API for finding out whether the host's native UI is in "dark mode" or "night mode" .

In UI design terms, "dark mode" is a relatively new idea. From what I can tell, it only really took off as an idea in 2018; see https://en.wikipedia.org/wiki/Light-on-dark_color_scheme. This was well after both Swing and JavaFX had stabilized. (In the case of Swing, decades after!)

There is a recent (December 2019) RFE in the OpenJDK Bug tracker for detecting the host OS's dark mode setting: JDK-8235460

In the absence of standard Java APIs, you could try OS specific ideas; e.g. as described in the following:

For Android dark / night mode detection is supported:

Stephen C
  • 669,072
  • 92
  • 771
  • 1,162
6

Yes, there are multiple ways to achieve this. Intellij Idea itself is written in Java, and she has an os theme synchronization functionality since version 2020.3.

You can use a library called jSystemThemeDetector. You have the ability to detect dark mode with it on Windows 10, MacOS and even on some Linux distributions.

Simple detection example:

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
}

Synchronization example:

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
    }
});
GyD
  • 61
  • 1
  • 1