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. It may be appropriate when you can optimize layout using application specific knowledge. Before building a custom layout, consider the following:
Unless you are writing a very generic layout that will be used by several Composite widgets, it is often simpler and easier to calculate sizes and position children in a resize listener. Many of the SWT custom widgets were written this way. Although a new widget can be implemented as a Composite/Layout pair, implementing it as a Composite that does its layout in a resize listener and computes its preferred size in computeSize is clearer, and does not involve writing an extra class.
If you still believe you need a custom layout class, it is a good idea to first implement the layout algorithm in a resize listener. This makes for simpler debugging of the algorithm itself. Be sure to test the various cases for layout: resizing smaller, larger, wrapping, and clipping. Once you have the algorithm working, the code can be refactored into a subclass of Layout.
Layouts are responsible for implementing two methods:
Further discussion of custom layouts can be found in Understanding Layouts in SWT.