Skip to main content

Baseline Layout

Posted by zixle on April 25, 2005 at 6:31 AM PDT

In many of the past Swing team brainstorming sessions we've lamented
the layout process. Layout managers are a necessary evil of cross
platform layout and resolution independence in that they allow
components to adjust in size and position based on external criteria.
Unfortunately the existing layout managers are not the easiest thing
to use and force the developer to write code that can be difficult to
read for a process that should be visual.

A friend speculated that Perl is best suited as a write once language.
That is, Perl code tends to be rather cryptic to read and hard to
maintain so that you end up writing the code once and praying you
never come back to it. Much of the layout code I've seen can be
lumped into that category. You write it once and if you ever come
back to it you have to pull out your hair trying to figure out the
intricacies of the containment hierarchy and layout managers being
used. Ugh! All this for a process that is inherently visual and
should be done in a tool.

This process has improved some what in recent years. In particular
forms layout helps for much
cleaner code, and some of the recent GUI builders have been getting
better. We've been working closely with the Netbeans folks as well
and will have an announcement soon. It's going to be fantastic. Stay

One area that we have been actively working on in Mustang is the
ability to get the baseline for various components. This doesn't help
developers laying out components, rather it's meant for those writing
layout managers. Aligning components along their baseline makes for
more polished apps. For example, if you have a label next to a button
you do not want them top aligned or bottom aligned, rather you want
them aligned on the baseline of the text.

Aligning components along their baseline also makes for great demos.
Look at Apple's Interface Builder, or recent versions of Visual Studio
for examples of GUI builders using baseline information to help
developers create visually pleasing apps.

Accomplishing baseline layout with Swing has been tough. This is for
a number of reasons, but primarily because Swing doesn't offer API to
determine where the baseline for a particular component is! Sure, we
do have the getAlignmentY method, but that really doesn't do all you
need and it was never wired up. I'm happy to report that we're nearly
done with the baseline API for Swing. If you want a sneak peek at
what the API is looking like head over to the javadesktop, in particular the thread on
Give us your two cents!


Related Topics >>