September 8th, 2007

Supporting GroupLayout baseline

GroupLayout was introduced in the middle of 2005 and later was added to JDK 6.0. It is the default layout manager used by NetBeans UI editor (code-named Matisse), and while the code using the GroupLayout tends to be very verbose and unreadable, the visual results are very pleasing as compared to other core Swing layout managers. For applications that require JDK 5.0, the swing-layout project provides JDK 5.0-compliant implementation.

This layout manager has introduced a concept of baseline that allows aligning components according to the perceived text baseline, instead of lining the components on the top border. This is especially relevant for form-based UIs that have grid-like arrangement of controls. In such UIs, you usually have labels in the first column followed by text fields, check boxes, combos and other controls in the subsequent columns. Here is an example:

As you can see, the alignment is far from perfect. The labels are incorrectly aligned with the text field, the combo and the password field. In addition, the labels are not spaced evenly. The implementation of GroupLayout includes hard-coded computation of baseline offsets for all core Swing components for the major core look-and-feels (Metal, Windows, Aqua and GTK). Here is how the same UI looks under Windows look-and-feel on Vista:

As you can see, the rows are aligned according to the text baseline, and the labels are equally spaced.

While this functionality is available out of the box for the core LAFs that were mentioned above and LAFs that extend those, there are three notable missing LAFs: Basic, Motif and Synth. Here is how the same UI looks under Motif:

What about the other two groups? There are a few third-party look-and-feels that extend either BasicLookAndFeel or SynthLookAndFeel. These include Alloy, Skin, Substance, Napkin, Liquid and Synthetica. Here are a few screenshots of these LAFs (Liquid, Napkin and Synthetica):

Following the request made by one of Substance users, the latest drop of Substance 4.1dev (code-named Lima) includes the new Substance Baselin plugin (that was refactored from the Substance NetBeans module). Once you add the substance-baseline.jar to your classpath, it will add the correct baseline alignment for applications that use GroupLayout from the swing-layout project under JDK 5.0.

Here are the before and after screenshots under Substance:

The license for this plugin is LGPL (the same as for the swing-layout.jar itself).

An astute reader would notice that the current implementation of JDK 5.0-compliant GroupLayout doesn’t provide builtin support for Synth-based LAFs. It would be interesting to see how Nimbus is going to address this issue, especially with the plans to backport it to JDK 5.0.