Library tutorials & articles

SWT and JFace, Part 1: A gentle introduction

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

SWT shell with shell trim

Figure 5. Shell with SWT.DIALOG_TRIM

SWT shell with dialog trim

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of SWT and JFace, Part 1: A gentle introduction.

Leave a comment

Sign in or Join us (it's free).

Barry Feigenbaum

Related podcasts

  • WebWork

    During this talk you'll receive an update on WebWork. WebWork is a Java web-application development framework. It is built specifically with developer productivity and code simplicity in mind, providing robust support for building reusable UI templates, such as form controls, UI themes, inter...

Events coming up

  • Dec 15

    Portland Java User Group

    Portland, United States

    This month's topic: TBD----------PJUG meetings start with eat+meet+greet time (pizza and beverages are provided), followed by the featured speaker, then some time for Q&A, discussion, and sometimes a drawing to give away swag. :)It is...

Want to stay in touch with what's going on? Follow us on twitter!