Composites
So far, we have been talking about individual controls. In most
GUIs, multiple controls are grouped together to provide a rich user
experience. In SWT, this grouping is implemented by using the Composite class.
Composites can be nested to any level, and can mix and match
controls and composites as children. This can greatly reduce GUI
development complexity and create opportunities for GUI code reuse (by
encapsulating the inner GUI). Composites can have borders and be easily
distinguished visually or can be borderless and seamlessly integrate
into even larger groups.
Listing 6 creates a bordered composite.
Listing 6. Create a bordered composite
import org.eclipse.swt.widgets.*;
:
Composite parent = ...;
:
Composite border = new Composite(parent, SWT.BORDER);
In addition to a border, the Group composite sub-class
supports a title. Groups are often used to contain radio-type buttons
as they define the set of exclusive buttons.
Listing 7 creates a bordered group.
Listing 7. Create a bordered group
import org.eclipse.swt.widgets.*;
:
Composite parent = ...;
:
Group border = new Group(parent, SWT.SHADOW_OUT);
border.setText("Group Description");
Shells
A shell is a top-level composite (frame or window) that may have no parent composite; instead, it has a Display as a parent, often set by default. Shells come in many styles, but the most popular are SWT.SHELL_TRIM or SWT.DIALOG_TRIM.
Shells may be modal or modeless. Modal shells, which are most often
used for dialogs, prevent the parent GUI (if any) from proceeding until
the child shell is closed.
Listing 8 creates a top-level nonmodal shell in frame style.
Listing 8. Create a top-level shell
import org.eclipse.swt.widgets.*;
:
Shell frame = new Shell(SWT.SHELL_TRIM);
:
Shells can have child shells. These are independent desktop windows
associated with the parent shell (i.e., if the parent is closed, all of
its children will also be closed).
Listing 9 creates a child shell in dialog style.
Listing 9. Create a dialog shell
:
Shell dialog = new Shell(frame, SWT.DIALOG_TRIM);
:
See Figure 4 shell with SWT.SHELL_TRIM and Figure 5 shell with SWT.DIALOG_TRIM to see how these values affect the shell trim.
Figure 4. Shell with SWT.SHELL_TRIM
Figure 5. Shell with SWT.DIALOG_TRIM