Swing idea

While diving into Xebece hacking I got an interesting idea for Swing. One major complaint against Swing apps is that they look very alien on most desktops. With newer JDK releases you can improve the situation a lot by using the native L&F of your platform, at least on Windows, GTK and MacOS X. The L&Fs are pretty good overall, by far not perfect but OK-ish. Except for the icons…

One thing that lets Swing applications still stand out very much is the integration (or lack of) of the native icon theme. For an application developer it is more or less impossible to integrate the standard toolbar and menu icons for things like File->Open, File->Exit etc. For example, in Gnome a theme usually also provides an icon theme for a whole bunch of stock icons. It would be nice to have a way to access these icons without doing lots of platform specific code in your application. One possible solution could look like this:

  • A defined set standard icons would be available via the LookAndFeel’s UIDefaults.getIcon(String) method.
  • The keys to access these icons would have to be documented and L&F implementations should provide these icons whenever possible.
  • Icons should be available for menus and toolbars at least. It might make sense to provide icons for a couple of fixed sizes too (as Gnome does).
  • There would have to be a sane naming scheme for the keys. For example, icon.file.open.16 for a 16×16 file-open icon. Or icon.file.open.toolbar for a file-open icon suitable for a toolbar.
  • L&F Implementations should use the native icon theme when possible, rather than using a fixed / built-in icon. For example, the GTK L&F implementation should use gtk_icon_theme_lookup_icon() to lookup the current theme’s standard icons.
  • The built-in actions (for example, the actions provided by the text EditorKits) could provide corresponding standard icons too.

Overall I think this shouldn’t be too hard to implement, and it would allow Swing applications to blend much better into the user’s desktop, without the need of lots of platform specific code in the application.


About Roman Kennke
JVM Hacker, Principal Software Engineer at Red Hat's OpenJDK team, Shenandoah GC project lead, Java Champion

6 Responses to Swing idea

  1. It’s like you’ve read our minds 🙂 I started a prototype workspace a few months ago that has support for “stock icons” in Swing, and it offers everything that you describe above. One thing is that GtkButtons almost always show an icon in addition to text, whereas on most other platforms (Aqua, Windows, Metal) this would look out of place. So I’m proposing a method on the LookAndFeel class that would say whether buttons should show an icon by default or not (this would be true on GTK, false otherwise). These changes would go a long way to making Swing apps look just like native GTK apps in the JDK 7 timeframe (too far down the road, unfortunately). While we’re at it, I’d like to include a nice looking stock icon set that would be available for the non-native L&Fs. Now all I need is some time to actually finish the work (that’s the hard part)!

  2. roman says:

    Very interesting! So is that stuff already included in current JDK7 builds? (And btw, my GtkButtons have the text turned off. This is something that can be configured by the user and I’d propose to adopt this setting in the GTKLookAndFeel class, rather than setting this to true).

  3. No, it’s not even close to being checked in, but something I’d like to get done in the coming months. I know what you’re talking about with the user settings for icon, text, icon+text; we don’t currently pick up those settings, but I agree it would be a worthwhile RFE for someone to work on (nudge nudge) 🙂

  4. roman says:

    Yeah yeah. Know what? You show me the code and I’ll do it 🙂

  5. Pingback: This note’s for you » Blog Archive » KDE vs Gnome and Java

  6. In the short term, check out GnomeStockIcon in http://software.jessies.org/salma-hayek/

    I’ve been using that to make my apps (wander around on jessies.org to see them) look better on GNOME. (There’s an annoying regression involving button sizes in Java 7 that I need to report, so this actually looks better in Java 6 still.)

    But, yeah, we need this for all OSes, and the GNOME “stock” concept is richer than mine, which really is just a short-term work-around.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: