Using Composites to Achieve Complex Designs

It is possible to use combinations of Composites or Groups together with layouts to your advantage when you need to construct more complex user interfaces than are possible with the single-container, single-layout approach. You can, for example, design a Shell that utilizes one type of layout and add to it Composite or Group objects that use a different type of layout. Using this combination approach, you can achieve almost any user interface design.

How do I do that?

Example 10-8 consists of three classes. The first is a Composite that makes use of RowLayout to display two SWT.PUSH-style Button objects:

Example 10-8. Using RowLayout on a Composite

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

public class RowComposite extends Composite {
    final Button okBtn;
    final Button cancelBtn;
    public RowComposite(Composite c)
        super(c, SWT.NO_FOCUS);
        RowLayout rl = new RowLayout( );
        rl.wrap = false;
        rl.pack = false;
        okBtn = new Button(this, SWT.BORDER | SWT.PUSH);
        okBtn.setSize(30, 20);
        cancelBtn = new Button(this, SWT.BORDER | SWT.PUSH);
        cancelBtn.setSize(30, 20);    
        cancelBtn.addSelectionListener(new SelectionAdapter( ) {
            public void widgetSelected(SelectionEvent e) {
                System.out.println("Cancel was clicked");

RowLayout is perfect for this composite, since the desired effect is that ...

Get SWT: A Developer's Notebook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.