Just as in Swing, SWT provides a number of core layout managers, as well as providing the opportunity to create your own custom layout from the org.eclipse.swt.layout.Layout base class.
FillLayout lays all widgets in a single continuous row or column. All widgets are forced to be the same size in this layout. Unlike Swing’s FlowLayout, FillLayout does not wrap, but you can specify margins and spacing. FillLayout is useful when a Composite only has one child, as it can cause the child of the composite to fill the shell.
FillLayout fillLayout = new FillLayout(SWT.VERTICAL); shell.setLayout(fillLayout);
RowLayout places components in horizontal rows or vertical columns within the parent Composite. Unlike FillLayout, RowLayout allows components to wrap and also provides margins and spacing. Rather than all components being the same size, each control can have its own parameters using the RowData object. A control can use this object through its setLayoutData method.
The most flexible layout manager in SWT is GridLayout, which lays components out in a grid formation. Each control that is placedin a composite using this layout can have an associated GridData object which configures the control. A control can use a GridData object through it’s setLayoutData method.
Note: GridData objects should not be reused between widgets, as it must be unique for each widget.
A grid can have a number of columns associated with it. As widgets are added they are laid out in the columns from left to right. A new row is created when the previous row has been filled. The following table illustrates the options in GridData:
|Specifies the location on the grid to place the component, with gridx=0, gridy=0 as the top left hand corner.
|Specifies whether the width or height of the widget will change depending on the size of the parent composite.
|The number of pixels to move in from the left or the top of the cell.
||The number of rows or columns that the widget will occupy.
||The preferred height or width of this widget.
||The minimum height or width of the widget.
||Informs the layout manager to ignore this widget when sizing and positioning controls
FormLayout positions children of a composite control by using FormAttachments to optionally configure the left, top, right and bottom edges of each child. Each child of a composite using FormLayout needs to have a FormData object with a FormAttachment.
Each side of a child control can be attached to a position in the parent composite, or to other controls within the Composite by creating instances of FormAttachment and setting them into the top, bottom, left, and right fields of the child’s FormData. If a side is not given an attachment, it is defined as not being attached to anything, causing the child to remain at its preferred size.
If a child is given no attachment on either the left or the right or top or bottom, it is automatically attached to the left and top of the composite respectively.