When an instance of GridLayout
is set on a Composite
(or a subclass of Composite
), all child controls will be arranged in a grid pattern. When there are multiple columns, the grid is populated from left to right, and from top to bottom.
In addition to specifying the number of columns, you can optionally adjust the margins around the grid, as well as the spacing between cells in the grid via various member variables.
Parameter | Details |
---|---|
numColumns | The number of columns in the grid |
makeColumnsEqualWidth | Whether or not the columns in the layout should be the same width |
When using a GridLayout
on a Composite
(or a subclass of Composite
), child controls should set their layout data to a unique instance of GridData
to dictate how the child should be displayed within the parent:
// ...
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Composite child = new Composite(shell, SWT.NONE);
child.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// ...
Failure to set a GridData
object on child controls may result in the child not being positioned as desired. Furthermore, if the child is a Composite
(or a subclass of Composite
), the child and its children may not be visible at all.
If the wrong layout data is used (eg. FormData
instead of GridData
), the result will be a ClassCastException
at runtime:
Exception in thread "main" java.lang.ClassCastException: org.eclipse.swt.layout.FormData cannot be cast to org.eclipse.swt.layout.GridData