SWT and JFace, Part 1: A gentle introduction

Layout managers

Composites often contain more than one control. These controls can be arranged in two ways:

  1. Absolute positioning -- Each control is set to an explicit X and Y position, and a certain width and height by your code.
  2. Managed positioning -- Each control's X, Y, width, and height is set by a LayoutManager .

In most situations, you would choose to use LayoutManagers, as they make adjusting to variable-size GUIs much easier. SWT provides several layout managers for you to use; in this installment of the series, we will talk about two of the more basic ones: FillLayout and GridLayout . In both cases, positioning takes place whenever the containing composite is resized.

Layout managers are uniquely assigned to a composite. Some layout managers are controlled by parameters just on themselves, and others also require additional parameters, LayoutData , specified on each control in the composite they manage.


FillLayout arranges controls in a row or a column. Each control is sized to be as wide or tall as needed to fill the composite, and space is divided evenly among the controls. A special case is when there is only one child control, in which case the control is sized to fill the entire parent composite.

Listing 10 creates a composite using a column FillLayout.

Listing 10. Create a column of controls using FillLayout

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layouts.*;
Composite composite = ...;
FillLayout fillLayout = new FillLayout(SWT.VERTICAL);


GridLayout offers a more powerful layout approach that resembles using HTML tables. It creates a 2-D grid of cells. Controls can be placed in one or more cells (called cell spanning). Cells can be made equal size or be given varying percentage of the grid's width or height. Controls are added to the next available column in a row; if no more columns exist in the row, the control moves to the first column of the next row.

Listing 11 creates a composite of two rows and two columns containing two labeled text fields. The columns can have different widths.

Listing 11. Create a table of controls

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layouts.*;
Composite composite = ...;
GridLayout gridLayout = new GridLayout(2, false);
Label l1 = new Label(composite, SWT.LEFT);
l1.settext("First Name: ");
Text first = new Text(composite, SWT.SINGLE);
Label l1 = new Label(composite, SWT.LEFT);
l2.setText("Last Name: ");
Text last = new Text(composite, SWT.SINGLE);


Consider the case where you need to specify how each control uses excess space within its cell. To provide this type of refined control for each cell, the controls added to a GridLayout managed composite can have instances of GridData (a LayoutData sub-class).

Listing 12 sets the text fields to take any excess available space (based on the prior listing).

Listing 12. Configure a layout that expands to all available space

first.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
last.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Building a running program

Now it is time to see all of the SWT controls we have discussed come together in a simple executable example: Basic1. See Resources for the complete source of this application.

SWT GUIs require a configured environment to run in. This environment is provided by a display instance, which provides access to the host operating system display device. The display allows you to process each user input (mouse or keyboard) to drive your GUI.

Listing 13 creates the environment, creates a GUI, then displays it.

Listing 13. Create and launch a GUI application

import org.eclipse.swt.widgets.*;
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Shell Title");

// *** construct Shell children here ***

shell.open(); // open shell for user access

// process all user input events
while(!shell.isDisposed()) {
// process the next event, wait when none available
if(!display.readAndDispatch()) {
display.dispose(); // must always clean up

This code creates a window similar to Figure 6.

Figure 6. Example Application

SWT application


In this first installment of the SWT and JFace series, I have introduced the most basic SWT widget controls: label, text, button, composite, and shell. These controls, combined with the display class, allow fully functional GUIs to be created.

Part 2 of this series will show you how to create menus, and how to use additional input controls and information display controls like list, tree, and table.

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger