13

I want to know if there is some way to get macOS Mojave style Dark Mode in a swing application in Java?

I want to get macOS Mojave Style Dark Mode on a JFrame in a Java Swing Application.

Please let me know.

Thanks

Asrar Bashir Sunge

Asrar Sunge
  • 173
  • 1
  • 7

3 Answers3

8

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.

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.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • `system` didn't work for me but `NSAppearanceNameAqua` and `NSAppearanceNameDarkAqua` did. – Indiana Kernick May 17 '21 at 02:00
  • @IndianaKernick Strange. Has worked fine for me. What Java version? And how did it not work (ie. Exception? Crash? No effect?)? – Harald K May 17 '21 at 07:17
  • I'm using Java 15 and macOS 10.13.1. It had no effect (showed light themed window). I would have preferred `system` but meh. `NSAppearanceNameDarkAqua` is fine for my purposes. – Indiana Kernick May 17 '21 at 07:30
  • At time of writing this (JDK 16.0.1, JDK 15.0.2), I can't get any of the recommended approaches to work (Tried `=system`, `=NSAppearanceNameDarkAqua`). Source code is here: https://gist.github.com/tresf/8d682cb4f0d56945c1c3132e9e7864c7. I'm setting the properties via IntelliJ's JVM properties. I'm on an M1 Mac, testing using Azul's build, if that matters. – tresf Jun 15 '21 at 18:55
  • I believe that matters. I only tested on Oracle builds. But it’s implemented in OpenJDK, so it could work. I guess you have to ask Azul why it doesn’t. – Harald K Jun 15 '21 at 19:22
  • On corretto 11.0.14.9.1, using `System.setProperty("apple.awt.application.appearance", "system")` appears to work. At least for the dark title bar (upon launch), the Look and Feel is still the regular Swing if it's not specifically customized. Also LaF and system bar won't change when the system UI changes. It needs something more involved, like this [darklaf](https://github.com/weisJ/darklaf/), and properly configured. – bric3 Jan 31 '22 at 09:34
6

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.

enter image description here

tresf
  • 7,103
  • 6
  • 40
  • 101
  • 1
    Since this is passed as a program argument, it might be necessary to ignore arg `-NSRequiresAquaSystemAppearance` and its immediate adjacent value `False` or `No` or `0` case insensitively. You'll then might see this system log `2022-01-31 10:14:21.435 java[66993:2679574] NSRequiresAquaSystemAppearance=NO` – bric3 Jan 31 '22 at 09:20
2

I guess, you might give it a try to use Darcula:

https://github.com/Revivius/nb-darcula

https://github.com/bulenkov/Darcula

import com.bulenkov.darcula.*;
...
...
BasicLookAndFeel darculaLookAndFeel = new DarculaLaf();
try {
  UIManager.setLookAndFeel(darculaLookAndFeel);
} catch (UnsupportedLookAndFeelException ex) {
  // ups!
}

enter image description here

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
  • 3
    That's nice for some cases, but it's not integrated with macOS. The title bar stays in light mode, and the app is stuck in "dark mode" when it should be responding to system theme changes. It also doesn't feel like a native app at all anymore since it's using the Darcula controls. – Baptiste Candellier Nov 26 '20 at 09:19
  • 1
    @BaptisteCandellier the title bar can be fixed via https://stackoverflow.com/a/56487943/3196753. – tresf Jun 15 '21 at 18:25
  • Is there a way to fix it via code? – George Shalvashvili May 06 '23 at 16:56