How to get macOS Mojave Style Dark Mode in Java in a Swing Application?
It seems the official solution to get dark mode is to use a system property. From JDK-8235363, this is available from JDK 14 and later:
A new system property is added:
"apple.awt.application.appearance"
to set the appearance of the whole java application.
- If no value is set then light mode will be used
- if
"system"
value is set then the current appearance of the macOS will be used- Other possible values are
"NSAppearanceNameAqua"
(the same as light), or"NSAppearanceNameDarkAqua"
(requesting dark) See https://developer.apple.com/documentation/appkit/nsappearancenameaqua?language=objc- If an incorrect value is set then light mode will be used
To get the best user experience, you should probably always use the system
value, to match the user's preferred setting:
-Dapple.awt.application.appearance=system
And of course test and make sure that you don't have your self-defined colors that makes text or components invisible or hard to see with this color scheme.
PS: If you are also looking for a Swing Look and Feel that matches the native macOS dark mode, have a look at VAqua. Disclaimer: I've never used it in a real application, but it looks extremely nice and uses native rendering to look just like the real thing.
To get the dark Title Bar, launch with option -NSRequiresAquaSystemAppearance False
. For example:
java -jar myapp.jar -NSRequiresAquaSystemAppearance False
# ("false", "no" or "0" will also work)
Note: Many solutions recommend editing the Info.plist
file, but those solutions didn't work for me.
Warning: Some frameworks can crash as a result of forcing this so use with caution.