Preferred size, actual size, and size options

Each LayoutData object implements the ILayoutElement interface. The ILayoutElement interface returns the preferred, default size, and resize behavior for the element and allows each element to be repositioned by the parent container. It provides information about how the control will look best, but it is up to the containing layout to make the final decision about the control's position and size.

The layout can automatically size and position the controls in the container at runtime according to the amount of available space in the parent container and the settings of the actual control. Instead of defining a static, fixed size, a control can allow the parent layout instance to position and place it according to the needs of the control, and the amount of space available within the parent. The layout determines a preferred size for each control in the container. The control's preferred size tells the layout and parent container that this is the size that the control was intended to be. The preferred size of a control is the minimum amount of space that is required to display the control within the parent container.

You can return the preferred size of any control that implements the Ilayout interface by calling a component's measure() function. The measure() function returns the preferred size of the control, given one or both dimensions that have been chosen by its parent. The preferred size may be totally different than the control's actual size. This is an indication of what size the control wants to be. The parent layout is still free to ignore this and set the control to a different size.

Another benefit of using a layout to arrange controls is that the layout will automatically adjust the size and position of a control each time a resize event occurs.

You can override the layout and set an explicit size and position for a control by specifying a LayoutData class. In the following example, a GridData object is instantiated and applied to a label. The margin is specified and the horizontal span is set to 2, meaning that the label will span across two cells within the grid layout.

myGridData = new GridData();
myGridData.hSpan = 2;
myGridData.marginLeft = 5;

labelHint = new Label();
labelHint.text = valShort;
labelHint.format = myTextFormat;
labelHint.wordWrap = true;
labelHint.layoutData = myGridData;

In addition to setting the span and the preferred height for a control, you can also set margins, and you can expand the width or height of a control by calling the expandWidth() or expandHeight() methods.

The ILayoutElement interface also defines how a control will grow or shrink at runtime. The SizeOptions class defines constants that determine how a control will grow, shrink, or grow and shrink both vertically and horizontally at runtime. You can call a LayoutData object's getSizeOptions() method to return a value that determines if the control wants to grow or shrink in a given direction. For example, the return value SizeOptions.SHRINK_HORIZONTAL indicates that if the control were made narrower than its preferred width, it would still be usable.

You can call the GridData object's setOptions() method to override the default behaviors of the layout and to control the growth options directly. For example, calling setOptions(SizeOptions.GROW_HORIZONTAL) will cause the GridData to always report itself as horizontally resizable even if it contains no horizontally resizable contents.