KappaLayout
LambdaLayout
Download :: Javadoc :: Resources
These layout managers are a result of a need for a layout manager with the
power of GridBag without the complexity. In the process of creating
such a layout manager, I've also managed to replace BorderLayout, GridLayout,
and BoxLayout at the same time. In writing KappaLayout, I had the following
goals:
- Ease of use. Must be easy to use when hand-coding user interfaces.
I've tried the various IDE's, for the most part, the code they generate has
to be maintained with their GUI builder, which may not even be a product
next year. Others use a proprietary file format for the user interface
that again can't be maintained with other tools.
- Allow "perfection". Must provide the ability to create "perfect"
layouts. "Perfect" layouts are those where each component is always
displayed at it's preferred size, or slightly larger to match the size of
a companion component. KappaLayout can do buttons perfectly:
the buttons are evaluated to determin which one has the largest preferred
size, then all other buttons in the group are set to that size.
- Powerful. Must support adding components explicitly and programmatically.
GridBag with it's GridBagConstraints is the only other layout that I know
of with this property.
- Control whitespace. Must allow whitespace to be added anywhere
to logically separate components.
- Ability to make components the same size. This is one thing that
I hate about the standard Java layout managers. The only layout manager
that makes components be the same size is GridLayout, and it doesn't pay
any attention to the preferred size of the component.
- Eliminate the need to nest panels with different layout managers to
get the right effect.
There are a couple of other features in KappaLayout that I wanted:
- The ability to add "padding" to components. I do quite a bit
of applet work, and tend to use AWT-only components. This means that
I can't use the nice javax.swing.Border classes, but often need to include
some whitespace around components.
- Stretching of components. Some components don't really have a
useful preferred size, like text fields and areas. These components
will give too-small-to-be-useful preferred sizes when first created, and
need to be stretch to a certain size to be able to display text adequately.
KappaLayout meets all of these goals and can indeed produce a "perfect" layout.
I don't claim that I thought of all these ideas myself, rather, I took the
good parts from several other layout managers. GridBag is the main
source for complexity. I like GridBag for it's ability to create intricate
layouts, I hate GridBag for it's inability to do it easily. KappaLayout
uses the concept of a constraint string, introduced in BorderLayout ("North",
"Center", etc), and carried to near perfection in TableLayout from Westhawk
(www.westhawk.co.uk). KappaLayout also has a constraints object similar
(but much simpler) to GridBagConstraints, which gives it the ability to add
components easily through code. Struts, an invisible component, were introduced
in Swing with Box and BoxLayout. Struts provide a very useful way of
putting some whitespace between components.
LambdaLayout grew out of the requests of users of KappaLayout. My example
of the double list with the two arrows for moving items from one list to
another didn't work the way that some people wanted it to. This is
an example that I am particularly proud of -- I spent a lot of time trying
to do this layout using the standard layout managers, and it never did look
right. I never did get those buttons correctly centered vertically
between the lists. My example was a "perfect" layout, all components
were placed once, at their preferred sizes, and were not allowed to change.
I received a number of requests from people who wanted the lists to stretch
when the frame was stretched. KappaLayout wouldn't allow this.
I looked at adding that functionality into KappaLayout, and found that by
doing so, I would lose the ability to create perfect layouts unless I added
quite a bit of complexity to the code and changed the constraint string properties.
Since this wouldn't work with existing components using KappaLayout, LambdaLayout
was created. The majority of the code for LambdaLayout comes straight
from KappaLayout, but the actually laying out of the components is quite
different. The same constraint string is used, which means LambdaLayout
is a drop in replacement for KappaLayout, and vice versa. LambdaLayout,
however, does not allow for a "perfect" layout. KappaLayout uses the
stretch parameter much differently than LambdaLayout. KappaLayout only
stretches a component when it is first laid out and never again. Conversely,
LambdaLayout will allow a component to stretch after initial layout.
I often mix the two, using KappaLayout for button panels and series of labels,
LambdaLayout for text areas and lists. KappaLayout tends to be used
more on dialogs, LambdaLayout on frames.
Following are some useful references for creating user interfaces:
Java Tutorial on using LayoutManagers:http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
Excellent book on usage-centered design, not just about user interface design,
but also about what not to do:
"Software for Use", by Larry L. Constantine and Lucy A. D. Lockwood, 1999,
ISBN 0-201-92478-1, http://www.foruse.com
The Java Look and Feel Guidelines:
http://java.sun.com/products/jlf/guidelines.html