Introduction

     

IBM created the Standard Widget Toolkit (SWT) as a replacement for the Abstract Windowing Toolkit (AWT) and Swing in Java. The SWT is a fully featured GUI API, and it comes built into Eclipse. There's a lot to cover in this and in the following two chapters. We're going to see some impressive code.

Java has had a long history of supporting GUIs. One of the early efforts was Java applets, and you can see an example in an Eclipse project, Applet , in Example 8-1.

Example 8-1. An example applet
 package org.cookbook.ch08; import java.applet.Applet; import java.awt.*; public class AppletClass extends Applet {     public void init( )     {         setBackground(Color.white);     }     public void start( )     {     }     public void paint(Graphics g)     {         g.drawString("Greetings from Applets.", 40, 100);     }     public void stop( )     {     }     public void destroy( )     {     } } 

Launch this applet from Eclipse by selecting Run Run As Java Applet. You can see the results in Figure 8-1.

Figure 8-1. A Java applet launched from Eclipse
figs/ecb_0801.gif

As an early GUI offering, Sun wrote the AWT in a few weeks and it was quite a success in its time. The AWT was easy to use and enabled Java developers to create rudimentary GUIs.

The AWT used the underlying operating system's controls. In Windows, for example, you'd see a Windows text box; on the Macintosh, you'd see a Mac text box. In fact, because the control sets in various operating systems differed, Sun only implemented a set of controls common to all platforms. And the AWT ended up being very limited for developers' needs.

To meet those growing needs, Sun created Swing, a GUI API that includes support for nonnative implementations of specialized controls such as trees and tables. You can see a Swing example in Example 8-2, the SwingApp Eclipse project, which you can find at this book's O'Reilly site in the Examples section.

Example 8-2. A Swing application
 package org.cookbook.ch08; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingClass extends JFrame {     Panel panel;     public SwingClass( )      {         super("Swing Example");         Container contentPane = getContentPane( );         panel = new Panel( );         contentPane.add(p);     }     public static void main(String args[])      {         final JFrame frame = new SwingClass( );         frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);         frame.setBounds(100, 100, 280, 250);         frame.setVisible(true);                  frame.addWindowListener(new WindowAdapter( ) {             public void windowClosing(WindowEvent e) {                 System.exit(0);             }         });     } } class Panel extends JPanel  {     Panel( )     {         setBackground(Color.white);     }          public void paintComponent (Graphics g)     {         super.paintComponent(g);         g.drawString("Greetings from Swing", 70, 100);     } } 

Launching a Swing application from Eclipse is no different from launching the applications we've already run. Just select Run Run As Java Application; you can see the results in Figure 8-2.

Figure 8-2. A Swing application launched from Eclipse
figs/ecb_0802.gif

Swing gives you a lot of power, but the results are Java-specific, at least visually. Sun tried adding an operating system-specific "look-and-feel" as an option, but in the end, Sun couldn't keep up with the rapid changes to all the operating systems. Also, Swing response times were slow compared to native implementations.

SWT comes to the rescue in many ways here. It supports a set of widgets that use native controls when such controls are available for use. If such controls aren't available, SWT uses its own widgets. SWT is a bold move on IBM's part; designed as it is to utterly replace AWT and Swing. And as you'd expect, SWT provides depth and a rich set of features.



Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net