How to make Java GUIs adhere to the current Qt theme

As [Feature Request] Adhere to system theme. · Issue #7548 · sp614x/optifine · GitHub explains, OptiFine Download (a graphical fidelity modification for Minecraft)'s GUI does not by default adhere to the theme of the system. I expected that this was an issue with it, although have wonderfully been corrected at [Feature Request] Adhere to system theme. · Issue #7548 · sp614x/optifine · GitHub

Recently, while troubleshooting another Java application with theming issues (this is not at all unique to OptiFine) I created a fix for this issue. Add

export _JAVA_OPTIONS="-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -Dswing.crossplatformlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true"

to your ~/.xprofile or another location that will allow the variable to be inherited by all processes in your desktop session. Your desktop’s application autostart feature may work, but if it has a dedicated place to set environment variables (for KDE, Session Environment Variables - KDE UserBase Wiki in the docs) use that. For Qt-centric desktop environments like KDE Plasma you may need to additionally set up a GTK theme.

This method obviously uses the GTK compatibility autoconfigurator, so although the colouration is correct, the elements themselves do not adhere to the Breeze application style (the usual disadvantages). However, it’s been a significant improvement to the Java GUIs I occasionally use.

I think this should be upstreamed, but perhaps KDE could implement this in some manner. Regardless, I’ve shared this here to inform those who do not yet know.


By the way, in case you want to test this for yourself and don’t find executing .jars intuitive, just adhere to command line - How can I execute a .jar file from the terminal - Ask Ubuntu and set a preset via Dolphin’s file extension to application associator.

It’s intended behavior by Oracle/Sun, Java apps created using Swing library don’t use any native graphical components from the system, it draws its own widgets and applies what is called Look And Feel (LaF) to them, this gives Swing apps the ability to look the same across different platforms Windows/Unix/Linux/MacOS, this is why Android Studio and most JetBrains IDEs look the same everywhere, and it provides also the ability to follow GTK3 theme on Linux by setting LaF to com.sun.java.swing.plaf.gtk.GTKLookAndFeel.

Only Java SWT library uses true native graphical components like what you see in Eclipse IDE.

While new JavaFX library has the same behavior as Swing but provides modern look.

Plus, that parameter set in env var _JAVA_OPTIONS for Swing apps will work only if the app hasn’t set it internally via code.

2 Likes