Introduction to Standard Widget Toolkit (II)

28 December 2007

The Standard Widget Toolkit (SWT) is a widget toolkit for Java developers that provides a portable API and tight integration with the underlying native OS GUI platform.

Many low level UI programming tasks are handled in higher layers of the Eclipse platform. For example, JFace viewers and actions provide implementations for the common interactions between applications and widgets. However, knowledge of SWT is important for understanding how the rest of the platform works.

Portability and platform integration

SWT defines a common portable API that is provided on all supported platforms, and implements the API on each platform using native widgets wherever possible. This allows the toolkit to immediately reflect any changes in the underlying OS GUI look and feel while maintaining a consistent programming model on all platforms.

Widgets

SWT includes many rich features, but a basic knowledge of the system’s core - widgets, layouts, and events - is all that is needed to implement useful and robust applications.

Widget application structure

When you are contributing UI elements using platform workbench extensions, the mechanics of starting up SWT are handled for you by the workbench.

If you are writing an SWT application from scratch outside of the workbench, you must understand more about SWT’s application structure.

A typical stand-alone SWT application has the following structure:

- Create a Display which represents an SWT session.
- Create one or more Shells which serve as the main window(s) for the application.
- Create any other widgets that are needed inside the shell.
- Initialize the sizes and other necessary state for the widgets. Register listeners for widget events that need to be handled.
- Open the shell window.
- Run the event dispatching loop until an exit condition occurs, which is typically when the main shell window is closed by the user.
- Dispose the display.

The following code snippet only displays the string “Hello World,” it does not need to register for any widget events.

   public static void main (String [] args) {
      Display display = new Display ();
      Shell shell = new Shell (display);
      Label label = new Label (shell, SWT.CENTER);
      label.setText ("Hello_world");
      label.setBounds (shell.getClientArea ());
      shell.open ();
      while (!shell.isDisposed ()) {
         if (!display.readAndDispatch ()) display.sleep ();
      }
      display.dispose ();
   }

In the next post, I will write about display, shell, parents and children and style bits.

del.icio.us:Introduction to Standard Widget Toolkit (II)  digg:Introduction to Standard Widget Toolkit (II)  spurl:Introduction to Standard Widget Toolkit (II)  wists:Introduction to Standard Widget Toolkit (II)  simpy:Introduction to Standard Widget Toolkit (II)  newsvine:Introduction to Standard Widget Toolkit (II)  blinklist:Introduction to Standard Widget Toolkit (II)  furl:Introduction to Standard Widget Toolkit (II)  reddit:Introduction to Standard Widget Toolkit (II)  fark:Introduction to Standard Widget Toolkit (II)  blogmarks:Introduction to Standard Widget Toolkit (II)  Y!:Introduction to Standard Widget Toolkit (II)  smarking:Introduction to Standard Widget Toolkit (II)  magnolia:Introduction to Standard Widget Toolkit (II)  segnalo:Introduction to Standard Widget Toolkit (II)  gifttagging:Introduction to Standard Widget Toolkit (II)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Introduction to Standard Widget Toolkit (II)

Leave a Reply