How to Write a Custom Look and Feel How to Write a Custom Look and Feel

by Kirill Grouchnikov
09/12/2006

Why Write a Custom Look and Feel?

There are four main reasons for writing a custom look and feel:

Where to Begin?

First, go to the Look and Feel repository to see if your idea has already been implemented. If it has, or even if there's a look and feel that is "almost but not exactly" like yours, consider joining the effort (especially if it's an open source project and you're comfortable with its license). You may first need to establish yourself as a worthy addition to the developer team by becoming acquainted with the source code, submitting bug reports with (at least partially working) fixes, and suggesting new features and the ways to implement them. This approach may not be easy (as with any other open-source projects), therefore you may consider starting your own look and feel project.

If you decide to start you own look and feel, you have three main decisions to make: the target platform(s), the target VM, and the license. Since the latter issue doesn't belong in this article, I'll focus on the first two.

Although Swing by its nature is lightweight and cross-platform, you may decide to target a specific OS or toolkit and perhaps even use some native code. Some of the existing look and feels do this (such as WinLAF, Quaqua, and KDE), confining themselves to the particular platform. You should consider not only the advantages of this approach (easier path to native fidelity) but also the deployment and maintainability issues (the need to install native code somewhere in the path, what happens when the native API changes and you want to support multiple OS versions, and so on). On the other hand, you will need to test your code only on the platform of your choice.

The target VM issue is complicated as well. Most of the currently active look and feels require Java 1.4.2. (A noteworthy exception is Simple, which requires 1.1.6.) Some look and feels require 5.0 as a minimum JRE, as Tiger was officially released almost two years ago and is in reasonably common use. The blog entry Porting small library from Java 5.0 to Java 1.4 - could it be any harder? describes some of the reasons why Substance has chosen 5.0 as the minimum requirement. One of the more compelling reasons is the many bug fixes since 1.4.2, along with the more flexible API provided for the painting extensions. A major drawback of deciding to go with 5.0 is cutting off users who have to use 1.4.2 (or earlier) VMs.

Another equally important decision is whether your look and feel will be image-based or Java2D-based. While some look and feels (such as Quaqua, Liquid, and obviously Synthetica) use bundled images to paint the controls, this approach may prove to be a disadvantage if you're planning to support theming and large controls as that will cause image scaling artifacts to be visible. However, it may be very difficult to achieve the same level of image-based design precision in Java2D.

What You Should Know

Writing a look and feel is neither simple nor quick. There are forty-four different UI delegates, and you would extend most of them in a complete custom look and feel. The codebase for Basic delegates weighs in at 1.9 MB, and at one point or another you will have to become well-versed in almost all of it. The codebase for Metal delegates weighs in at an additional 680 KB, although if you're planning to provide a complete custom look and feel that doesn't use any painting or listening logic from Metal delegates, you don't need to extend Metal (as Alloy, Liquid, Napkin, Skin, and Substance do). In addition to writing your delegates, sometimes you'll be forced to fix bugs in the core delegates, especially if this action is requested by your users.

Ideally, you should be very well-versed in the following topics:

Note that some of these areas can be learned while you're writing your look and feel. However, you shouldn't embark on writing a custom look and feel as your first Swing project; most probably you will be overwhelmed by the technical details. However, if you do persist in writing your own look and feel, you will learn a lot of invaluable information about how Swing works and how to use it correctly. Doing it all the way will surely make you an outstanding Swing expert.

Basic Guidelines

Think about the developers who will be using your L&F.

Think about the users who will be using your L&F.

Follow sensible programming practices.

Know the L&F-specific development issues.

Conclusion

Writing a look and feel provides many benefits. You will learn Swing thoroughly since you will be exposed to both its internals and how people use it (sometimes very inventively) in their applications. Unlike the average Swing programmer who is exposed only to the public API, you will be able to interact with the Swing API at a much lower level, having more powerful tools at your disposal. You will be able to give your in-house applications a distinct UI that goes far beyond simple theming.

This article describes many of the questions, issues, and problems that you will face during a look and feel development cycle.

Resources

Kirill Grouchnikov has been writing software since he was in junior high school, and after finishing his BSc in computer science, he happily continues doing it for a living. His main fields of interest are desktop applications, imaging algorithms, and advanced UI technologies.


 Feed java.net RSS Feeds