ARE YOU a Java programmer who wants to know a good way to manage the layout of display components (e.g. buttons, lists, text areas, text fields, etc.)? This tutorial page is intended to show you how to solve this problem using XmFormLayout.

XMFORMLAYOUT is a freeware Java layout manager that arranges an applet's display components according to constraints. It was inspired by Motif's XmForm widget, and has similar functionality (except that it works with Java not Motif).

A CONSTRAiNT is a mathematical equation which describes the placement of one component relative to another. For example the constraint

"Tweedle.left=Humpty.left"

means that the left side of the component named Tweedle is in the same place as the left side of the Humpty coordinate.

In XmFormLayout, all constraints have this format:

<component>.<attribute>=<component>.<attribute>

AN ATTRiBUTE is a window coordinates used to position a component. There are six:

  • left,
  • right,
  • top,
  • bottom,
  • hcenter, and
  • vcenter
The left and right attributes refer to the x coordinate on the left side and right side respectively of a component. The top and bottom attributes refer to the y coordinate on the top side and bottom side respectively of a component. The hcenter attribute refers to the x coordinate at the midpoint between the left and right attribute. The vcenter attribute refers to the y coordinate directly between the top and bottom attribute.

AN OFFSET may be applied to a constraint to provide greater flexibility in placement. For example the following constraint contains an offset of 30 pixels:

"Dumpty.left=Humpty.right+30"

This means that the left side of Dumpty is 30 pixels to the right of the right side of Humpty. (Horizontal offsets increase in the rightward direction, vertical offsets increase in the downward direction. To move in the opposite direction, simply specify a negative offset.) Offsets may only appear on the right side of the constraint (that is, the "rvalue"), never on the left side (the "lvalue").

THE FORM is a well-known component which encloses all of the other components, it is the enclosing window. For the most part it is treated like any other component. The following example shows how to make the left side of Humpty be 21 pixels from the left side of the form.

"Humpty.left=form.left+21"

NESTED LAYOUTS are possible, but it can be tricky to develop a nested system of constraints which is rapidly solvable. It is usually best to reduce or eliminate nesting when possible.

TYPICAL PROBLEMS with XmFormLayout are referencing components in the list of constraints which do not actually exist, or failing to provide a list of constraints which adequetely specifies the placement of all components.

ADDiTiONAL information about layout managers may be found in Layout Manager Launch, a web page entirely devoted to "legendary links level-headed lessons and lively lunacy about layout managers."