SWT FormLayout (Professional Version Only)

FormLayout is also one of the most useful and powerful of the standard layouts, but it is also one of the most complicated. With a FormLayout, the widget children of a Composite are laid out using various constraints applied to each side of the widget. FormLayout has a number of configuration options, and, like GridLayout, the widgets it lays out can have an associated layout data object, called FormData which holds onto an instance of FormAttachment for each side of the widget that has a constraint.

   

Play movie. Requires a web connection.
Play Movie

The power of FormLayout lies in the ability to configure the FormAttachment for each side of a widget controlled by the FormLayout.

FormLayout, FormData and FormAttachment are fully supported by Designer. You can design your forms using only the mouse (or assist with the keyboard for even faster design).

To use FormLayout, select it in the control palette and drop on a Composite.

Once a widget has been dropped it can be freely dragged around the composite. Dynamic snap points will appear at the window margins, at predefined percentage points and near other widgets. Each side of the widget can be independently attached to the near or far side of the window, to a specific percentage line within the window or to the near or far side of another widget. The attachment style on each side of the widget is preserved while dragging the widget unless a snap point is hit that will change the attachment style. The attachment style on each side may also be change by the popup graphical attachment menus on each side.

The icons shown on each side of the widget indicate the current attachment style for that side of the widget:

- a fixed offset from the left side of the window
- a fixed offset from the right side of the window
- a fixed offset from a widget
- a percentage offset
- a fixed offset from a percentage point (e.g., 5 pixels from the window centerline)

There are also hot keys, which can be used for even faster changing of layout properties. Select a control on the design surface and use hot keys:

  • First letter: L, R, T, B selects attachment.

  • Next letter: M - set attachment to margin.

  • Next letter: A - work with absolute pixels,

  • L or R - from left or right side of parent, then several digits, if without L or R, then L, after Enter make changes.

  • Next letter: P - work with percents,

  • A - set to percent with zero offset,

  • S - set to percent with offset negative for this attachment, then several digits, if without A or S, then with standard offset for this attachment, after two digits or one and Enter make changes.

Margin Attachments

  • This shows a button that has been attached to the top-left corner with a fixed offset.

  • The blue lines indicate the margin snap point (configurable via the preference page).

Percentage Attachments

  • This shows a button being dragged to a percentage offset at 33% of the windows height and width

  • The gray lines indicate preferred percent snap points (configurable via the preference page).

  • The blue lines indicate fixed offsets from the percent lines (configurable via the preference page).

  • The widget may be snapped to either the percent line or to the offset line

  • Drag feedback is provided on the borders indicating the current percent and offset.

  • When a drag operation is initiated, the select widget quadrant is used as a hint to decide which dynamic snap points to use

  • If a snap point is selected, the relevant attachments will be changed a necessary

Widget Attachments

  • This shows a button being dragged next to another button to establish a widget snap point

  • When a widget is dragged near a target widget, dynamic snap points appear in three places relative to the target widget

    • The near edge of the target widget

    • Fixed offset from near edge of target widget

    • The far edge of the target widget

  • The gray lines indicate snap points at the edge of a widget

  • The blue lines indicate fixed offsets from the near sides of a widget (configurable via the preference page).

  • Once a widget attachment has been established, moving or sizing a target widget will also cause any attached widgets to move or size as appropriate

  • When a drag operation is initiated, the select widget quadrant is used as a hint to decide which dynamic snap points to use

FormLayout Preferences

  • This shows the various preference options available for configuring FormLayout support

  • Default snap points may be established for the window margins, fixed percentage offsets and (near-side) widget offsets

  • The line colors used for the snap points may be set

  • Preferences may be independently controlled for horizontal and vertical operations

  • When dragging or sizing, the sensitivity to snap points may be set (e.g., how close do you need to get before they grab you)

  • Default preference snap points may be established (50% would correspond to the centerline of the window). Other useful choices might be 25%, 33%, 66% and 75%.

  • Entering a value of "-1" for any offset value will suppress snap points corresponding to that attachment style (e.g., setting the Widget offset to -1 would disable the widget snap points).

FormLayout Example

This example shows two button in a FormLayout. The first button is attached 50 pixels from the top-left corner of the window, 200 pixels from the right-hand side of the window, and 0 pixels from the vertical centerline of the window (the 50% point). The second widget is attached 50 pixels from the bottom-right corner, 20 pixels from the right side of the first button and its top is aligned with the top of the first button.

As you can see, there is a setLayoutData() call for each button control with the FormData defining the attachments on each side.