Comparing Swing to the AWT

   

Although you won't be exploring Swing until the next chapter, you might want to see an example of how Swing compares to the AWT. AWT and Swing versions of a small application are presented here. The first version is called AWTHello, and its source code is presented in Listing 15.2.

Listing 15.2 The AWTHello Application Source Code
 // AWTHello.java import java.awt.*; import java.awt.event.*; class AWTHello extends Frame implements ActionListener {    Button b;    static String [] labels =    {       "Hello! Welcome to AWT!",       "You cannot assign icons to AWT buttons."    };    int nextLabel;    AWTHello (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       Panel p = new Panel ();       b = new Button (labels [nextLabel++]);       b.addActionListener (this);       p.add (b);       add (p);       setSize (300, 100);       setVisible (true);    }    public void actionPerformed (ActionEvent e)    {       b.setLabel (labels [nextLabel++]);       validate ();       nextLabel %= labels.length;    }    public static void main (String [] args)    {       new AWTHello ("Hello, AWT Version");    } } 

AWTHello builds a GUI consisting of a single button. This button is initialized to some default text, which changes when the button is pressed. Figure 15.1 shows AWTHello 's GUI.

Figure 15.1. AWTHello 's GUI consists of a button initialized to default text.

graphics/15fig01.gif

Much of AWTHello 's source code should make sense (now that you've worked your way through the previous chapter). However, you might be wondering why validate is called in actionPerformed.

The validate method is called to inform the container's current layout manager that it must re-layout all components added to the container. If validate is not called, the button's new label will be displayed using the existing dimensions of the button. A large label would be truncated at the button's edges. By informing the layout manager that something has changed (by calling validate ), it will have a chance to resize the button to a more appropriate size . (If you don't call validate, you'll only see a resized button by physically resizing the button's frame container.)

To see what the Swing equivalent of this program looks like, check out Listing 15.3, which presents SwingHello 's source code.

Listing 15.3 The SwingHello Application Source Code
 // SwingHello.java import javax.swing.*; import java.awt.event.*; class SwingHello extends JFrame implements ActionListener {    JButton b;    static String [] labels =    {       "Hello! Welcome to Swing!",       "You can assign icons to Swing buttons."    };    int nextLabel;    SwingHello (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          });       JPanel p = new JPanel ();       b = new JButton (labels [nextLabel++],                        new ImageIcon ("bullet.gif"));       b.addActionListener (this);       p.add (b);       setContentPane (p);       setSize (300, 100);       setVisible (true);    }    public void actionPerformed (ActionEvent e)    {       b.setText (labels [nextLabel++]);       nextLabel %= labels.length;    }    public static void main (String [] args)    {       new SwingHello ("Hello, Swing Version");    } } 

Caution

When running SwingHello under version 1.2.2 of the Java 2 Platform Standard Edition on Windows 2000, you must explicitly specify the path to bullet.gif.


As with AWTHello, SwingHello also builds a GUI consisting of only one button. This GUI is shown in Figure 15.2.

Figure 15.2. Like AWTHello, SwingHello 's GUI consists of a button initialized to default text.

graphics/15fig02.gif

Compare Figures 15.1 and 15.2. It's easy to see the differences. Among these differences, there's an icon on the Swing button, which is something that cannot be done with AWT buttons. You've compared GUIs, so now compare SwingHello 's source code to AWTHello 's source code.

To begin, notice that instead of an import java.awt.*; directive, SwingHello begins with an import javax.swing.*; directive. After you start to create Swing programs, you'll use this directive to import Swing classes and interfaces.

SwingHello extends the JFrame class, instead of extending Frame. This turns SwingHello into a Swing application. (If you check the JDK documentation, you'll discover that JFrame extends Frame. )

AWTHello references Button and Panel classes, whereas SwingHello references JButton and JPanel classes. If you look through the JDK documentation, you'll find that all the AWT's component/container classes have equivalent Swing classes. (The letter J differentiates Swing classes from AWT classes and stands for Java.)

Unlike the AWT's Button class, Swing's JButton class allows you to attach an icon to a button. This is accomplished by creating an instance of Swing's ImageIcon class, and passing this instance to one of JButton 's constructors.

AWTHello allows you to add a panel directly to its AWT frame container. You cannot do this with SwingHello. Instead, you add the panel to its Swing frame's content pane. This pane serves as an intermediate container that is part of a Swing GUI's layered architecture.

The Button class declares a method called setLabel for setting an AWT button's text. However, this method is marked deprecated by JButton 's AbstractButton superclass. Therefore, you must call JButton 's setText method (inherited from this superclass) to set a Swing button's text.

Finally, you'll notice that the validate method is not called in SwingHello 's actionPerformed method. Unlike the AWT, Swing ensures that the current layout manager is called when a component's size and/or position changes.

As you've just seen, Swing and AWT programs are very similar. By building onto the AWT's architecture, Swing's designers made it easy for AWT developers to migrate over to Swing. Although it's not a good idea to include the AWT's components and containers in a Swing program, there is no reason why you cannot use the AWT's layout managers, events, and listeners.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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