How To Make Java Swing Look Better

This is not new information but I’ve been having an increasingly difficult time finding the exact options to use in the library of babel that is the modern search engine. So here we are.

There’s 4 settings at play here. awt.useSystemAAFontSettings and swing.aatext let you force enable (or disable!) text anti-aliasing. swing.defaultlaf and swing.crossplatformlaf let you set the theme. You can install your own themes but I don’t remember how to do that so I’m not writing about that today. The default theme is Metal. That’s the thing that looks distinctly like an oldschool java application. If you don’t like that, that’s probably why you’re on this post.

If you’re running java as a command, and you want to use your system’s GTK theme for java, here’s what you do:

java -Dawt.useSystemAAFontSettings=on \
     -Dswing.aatext=true \
     -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel \
     -Dswing.crossplatformlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel \
     -jar JamochaMUD.jar

This is usually what I’m trying to do because it’s the easiest way for me to get a dark mode in applications. Be aware if you are going for a dark mode that this doesn’t set the icon theme, so you might get clashing icons. I think there’s a way to override the icon theme? IDK what it is, if you know please contact me.

Notice defaultlaf and crossplatformlaf are seemingly duplicating the theme settings. That’s because some applications have code in theme that queries the crossplatformlaf setting and sets the appearance to that instead of using the default. That’s nice of them, but we’re trying to set our own theme here.

There’s other themes you can use. Here’s the bundled “Look and Feel”s, as they’re called.

Just set -Dswing.defaultlaf and -Dswing.crossplatformlaf to whatever you want. Or if you like Metal but you want to change the colors to be even more oldschool, -Dswing.metalTheme=steel.

If you want to use these settings by default, you can export an environment variable called _JAVA_OPTIONS which contains these. For example,

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

Note that some programs won’t listen to this setting if they set their own Look and Feel in their code. If you’re really serious about theming those you’ll need to learn how to do java modding, decompiling their code or modifying the bytecode. It’s less scary than it sounds, but out of scope for this post.