posted by Andy Roberts on Thu 19th May 2005 19:33 UTC

"Java look and feel, Page 2/2"
The solution here is quite simple, use a different LAF! Because of the pluggable LAF facilities in Swing, it is possible to use third-party LAFs instead. Of course, some are commercial offerings, others are free. Possibly the best known freely available LAFs are those released by Karsten Lentzsch called JGoodies Looks. Let's look at a sample Swing application using the old Metal and the new Ocean cross-platform LAFs.

Screenshot of a Swing application using Java's old Metal look-and-feel.
Screenshot of a Swing application using Java's Ocean look-and-feel, which should be an improvement over Metal to most viewers.

Ocean is an improvement, that's for sure. Now, let's see what happens when we use the Plastic LAF:

Screenshot of a Swing application using Platic look-and-feel, which should be more appealing to most viewers.

Now we have a perfectly pleasing LAF for any platform. JLooks also provides a themes mechanism that allows you to specify different colour schemes. By default, it tried to match the desktop colours. Also, it doesn't take very much effort to get your Java application to look like this. LAFs can be set in the program code directly with a couple of lines of code. Alternatively, you can actually specify as a command-line option, although it does require you having the appropriate LAF classes that you want to use (if third party).

Fonts

Truthfully, the lack of anti-aliased fonts in Swing mean that it still stands out like a sore thumb. Yet, there is support - it's just not enabled by default! This is a funny issue because the underlying graphics framework, Java2D, which actually draws the Swing components has supported anti-aliased fonts for years, although work is in progress to enhance this for better sub-pixel support to improve text display on LCDs. As far as I can tell, the reason why it's not enabled for Swing components by default is mainly for legacy reasons, as Romain pointed out to me:

"Anti-aliased text does not have the same size as non-AA text and that can break layouts and legacy apps."

There are many, including Karsten and Romain, who believe that because the fonts used in GUIs are typically small, e.g., ~8-10pt, anti-aliasing can actually make text more difficult to read, and therefore only support the idea of anti-aliasing for larger sizes. There is some truth to this, but I think there is a certain amount of subjectivity too, and so I personally think it better to make it simple to enable/disable anti-aliased text as necessary.

Unfortunately, this hasn't been simple. The old way required you to sub-class each component you wished to be anti-aliased and override the paint() method. This is clearly an inconvenient approach, although, the SmoothMetal project did just this and extended Java's Metal LAF. There is another approach used by the Wrap LAF project that allows you to achieve global anti-aliased text by adding a wrapper around the bundled LAFs. As of Java 1.5, an undocumented class contained a way of setting each component to enable anti-aliased text. Better, but still required doing for each component you used, i.e., not global. The only global way (without external libraries) is also in Java 1.5 and that is an undocumented Swing property called "swing.aatext". Set this to equal 'true' (and this can be done from the command-line when you launch an app) and all Swing components will be anti-aliased. It's worth noting that Swing lead Scott Violet has said, "swing.aatext is not a documented or supported property. It's meant for internal testing and may go away at any point." Of course, he said in another post, "While it may go away it'll be replaced by something better :)"

In the meantime, it'll do just fine! Here's our sample app again, with anti-aliased fonts enabled.

Screenshot of a Swing application using Platic look-and-feel and with anti-aliased text.

Synth: the future of LAFs?

Creating your own LAFs requires a lot of knowledge about Swing, and is very programming intensive. Synth is a new look-and-feel that shipped with Java 1.5. It's still pretty new and documentation is not comprehensive on this yet. Here's Romain on Synth:

"The Synth look and feel is a generic look and feel, a kind of skins-engine. Its purpose is to let you create a whole look and feel only with an XML files and pictures. It is much simpler than creating a "real" look and feel which is a very complex and tedious task. And it actually allows you to create real look and feels. In Java 1.5, the GTK look and feel is Synth based. Unfortunately, Synth lacks documentation and examples but this issue will eventually be addressed in the future. Also, Synth might unfortunately require some programming work. Due to the nature of the Synth resources, XML files, you're not free to do everything you want. When something's missing, you have to crank out some code."

This approach does look interesting, as it reduces considerably the burden required to get a LAF up-and-running, and is more analogous to skinnable applications like Winamp. There are of course performance issues to think about: a GUI consisting of images could well require additional resources, memory wise, although, with the improved graphics pipeline in the forthcoming Java release, this may not be an issue. A good example of a Synth-based LAF is Synthetica.

Screenshot of the Synthetica look-and-feel demonstrating Synth.

Summary

In conclusion, I think it's fair to say that the bundled LAFs in Java are getting better, but still not pleasing to the majority. I have no doubt that the Java team is working hard on this. However, whilst there is room for improvement, I think this is a good time for many developers to reflect think about whether they really need true platform fidelity, when there are other perfectly decent third-party LAFs available. They will look different, but, so do many applications!

The "ugly" Java is something of a myth nowadays. Whilst Swing doesn't exactly shine aesthetically by default, there's not a lot of effort required to plug in a good looking LAF, and with Synth we can expect more LAFs to appear (and hopefully some will be decent!) There are plenty of Java applications that demonstrate how great Swing can look, Xerto's Imagery is possibly my favourite example at the moment, although the JIDE products are pretty impressive. Also, Javootoo maintains a list of free and commercial LAFs.

About the author:
Andrew Roberts is a computer science graduate from the University of Leeds, UK. He remained at Leeds to study further towards a PhD in Natural Language Processing. He has been using Linux for almost 8 years, and Java has been his language of choice for advanced language processing and machine learning during the last three years.
If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.
Table of contents
  1. "Java look and feel, Page 1/2"
  2. "Java look and feel, Page 2/2"
e p (0)    89 Comment(s)

Technology White Papers

See More