Traditionally, a widget is thought of as an abstract device that is useful for a particular purpose. The term is popular in economics. If you have ever studied economics in university, the professor probably asked, "How does an increase in supply affect the price of widgets?" when discussing the laws of supply and demand. We software developers have co-opted this word to represent those self-contained packages of code that are used to build most modern graphical user interfaces. SWT is called the Standard Widget Toolkit because widgets really are the basis of any application built in SWT.
Because widgets are so fundamental to developing applications, they are covered in detail in this part of the book. Each chapter covers a different aspect of the topic.
Widget Fundamentals:
an overview of widgets and user interaction
The Keyboard:
interacting with widgets via the keyboard
The Mouse:
interacting with widgets via the mouse
Control Fundamentals:
behaviors that widgets called controls share
Display:
the connection between widgets and the underlying platform
Native Widgets:
an overview of the native widgets
Basic Controls:
the simplest controls in SWT
Tool Bars and Menus:
controls that perform actions
Advanced Controls:
tree, table, and tab folder controls
Range-Based Controls:
controls that describe numeric ranges
Controls, Composites, Groups, and Shells:
the container controls
Canvas and Caret:
drawing area controls
Draggable Controls:
controls that can manipulate the user interface
Dialogs:
self-contained information windows and prompters
Layout:
widget positioning and resizing support
The first four chapters describe what widgets are, their life cycle, and how users interact with them.
The Display chapter covers the class that represents the root of all widgets. Both its relationship to the rest of SWT and the specific API it provides are covered.
The Native Widgets chapter lists and briefly describes each of the native widgets. Following this chapter, there are a number of chapters that together provide complete descriptions of all of the native widgets provided by SWT: Basic Controls; Tool Bars and Menus; Advanced Controls; Range-Based Controls; Controls, Composites, Groups, and Shells; Canvas and Caret; and Draggable Controls.
The remaining two chapters provide both general descriptions of dialogs and layouts; they also have specific sections on each of the dialogs and layouts that are included with SWT.
If You Don't Read Anything Else…
If you are one of those people who learn by reading code and running it, this section is for you. In it, you will find "HelloWorld," a minimal SWT program with a short description of each line of code and a list of the widget packages. If you want to run this code, you should read the section Running SWT Applications, but that's it. Enjoy!
HelloWorld in SWT
Here once again is a complete SWT program that creates and displays a new window on the desktop with "Hello, World." in the title bar. This is a slight variation of the one described in the Using SWT chapter. Figure P1.1 shows the result of running this program on Windows XP.
01 import org.eclipse.swt.widgets.*;
02 public class HelloWorld {
03 public static void main(String[] args) {
04 Display display = new Display();
05 Shell shell = new Shell(display);
06 shell.setText("Hello, World.");
07 shell.setSize(200, 100);
08 shell.open();
09 while (!shell.isDisposed()) {
10 if (!display.readAndDispatch())
11 display.sleep();
12 }
13 display.dispose();
14 }
}
This is what the code does.
Line 1
Most widgets are found in the package org.eclipse.swt .widgets (native widgets). The other packages that you will need to investigate are org.eclipse.swt (constants), org.eclipse.swt.events (typed events), org.eclipse.swt.layout (layout algorithms), and org .eclipse.swt.custom (nonnative widgets).
Line 4
Every SWT program must create a display to establish the connection between SWT and the underlying window system. For readers who are familiar with the X Windows system, a display is equivalent to an X Windows display. You need to create a display before you can do anything interesting with SWT.
Lines 5–7
These lines create a shell, then set its title and size. Top-level windows are instances of the class Shell and are created on a display. It is not necessary to set the title, position, or size of a shell when it is created. If they are not set, the title will be empty, and the initial position and size will be chosen by the operating system.
Line 8
Shells are invisible when created. This line makes the shell visible, brings it to the front on the desktop, and sets user input so that when keys are typed, they go to the shell.
Lines 9–11
SWT supports an event-driven user interface that requires an explicit event loop. These lines repeatedly read and dispatch the next user interface event from the operating system. When there are no more events, the program goes to sleep waiting for the next event, yielding the CPU. The loop terminates when the programmer decides that the program has ended, typically when the application's main window is closed.
Line 13
This line disposes of the display. Strictly speaking, it is not necessary to dispose of the display because this will happen when the program exits to the operating system. However, it is bad form not to do this because explicitly disposing of the display allows the full life cycle to complete.
As long as the shell remains open, the event loop keeps Java alive, and the SWT program keeps running. When the shell is closed, the program terminates.

Obviously, there is much more to using SWT than this, but almost all SWT programs have this same basic shape. The many complete examples included in the source code that is provided with this book are also good starting points for exploration. Run them, change them, and watch the results.
You should also feel free to read the rest of the book. It provides many tips and tricks that are subtle enough that it is worth taking the easy road to discover them.