Custom layouts
Occasionally you may need to write your own custom
Layout
class. This is most appropriate when you have a complex layout that is used
in many different places in your application. Note that unless you are
writing a very generic layout that will be used by several
Composite
widgets, it is sometimes simpler and easier to calculate sizes and position
children in a resize listener.
Layouts are responsible for implementing two methods:
-
computeSize(...) calculates the width and height of a
rectangle that encloses all of the composite's children once they have
been sized and placed according to the layout algorithm. The hint
parameters allow the width and/or height to be constrained. For example,
a layout may choose to grow in one dimension if constrained in another.
-
layout(...) positions and sizes the composite's children.
A layout can choose to cache layout-related information, such as the
preferred extent of each of the children. The flushCache
parameter tells the
Layout
to flush cached data, which is necessary when other factors besides the
size of the composite have changed, such as the creation or removal of
children, or a change in the widget's font.
A third method, flushCache(...), can be optionally
implemented to clear any cached data associated with a specific control.
Often, the computeSize() method of a widget can be expensive, and so layouts
can cache results to improve performance.
Further discussion of custom layouts can be found in
Understanding
layouts in SWT.