Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect whether Mac OS or Windows OS is in Dark Mode in Java

Tags:

java

javafx

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.

like image 803
Ehsan Waheed Avatar asked Jan 24 '26 01:01

Ehsan Waheed


1 Answers

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
    }
});
like image 73
GyD Avatar answered Jan 26 '26 16:01

GyD