Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The first difference you’ll notice between HelloPlus and HelloApplication is that we’ve added several data members to the class: fButton, fTextField, and so forth. Often you’ll need to refer back to user-interface components during the life of the program—it’s convenient to have all the components available as class members. (If you don’t want to carry these references around in the class, you don’t have to—theChoice is an example of a user interface component that is not referenced as an instance variable.)

The biggest area of change is in init(). We’ve used init() to build the user interface—that way, the components are added to the applet when we’re running as an applet, and the application frame when we’re running as an application.

Understanding fTextField and fLabelForTextField We start init() by adding our ubiquitous Hello, World! label. Then we instantiate a new TextField and add it to the interface. We follow this with a new Label, likewise added to the interface. Now go down to lines 72 through 77:

fTextField.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
          fLabelForTextField.setText(“Your text is “ + 
                                   fTextField.getText());
       }
     });

Here’s a construct we haven’t seen before. Because we added a TextField, we’re able to get a semantic event—the ActionEvent—which has only one method: actionPerformed(). Although the action differs from one component to the next, the method for listening to these events is the same. We don’t need an adapter because only one method is in the event. Instead, we declare a new ActionListener and define it right here inside addActionListener(). In this case, the body of the method is quite simple:

fLabelForTextField.setText(“Your text is “ + 
                                   fTextField.getText());

When you change the text in the text field and press Enter, the Java runtime sends an ActionEvent to your listening program, which changes the text in fLabelForTextField.


The components in HelloPlus.java don’t need adapters because their events have only one method. If you want to receive events with more than one method, consider using adapters—abstract classes that implement an interface and “stub out” the methods.

Understanding fButton This version of the program starts with a red background on the frame and a white button labeled “White.” When you click the button, it sends an ActionEvent to the listening program. The definition of the listener starts at line 43 of Listing 38.3:

fButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        if (fButton.getLabel() == “White”) {
          setBackground(java.awt.Color.white);
          fButton.setLabel(“Red”);
        }
        else {
          setBackground(java.awt.Color.red);
          fButton.setLabel(“White”);
        }
        Component theComponents[] = getComponents();
        try {
          if (theComponents.length == 0)
            throw (new AWTException(“Cannot find the components”));
        } catch (AWTException theException) {
          System.err.println(“Exception: “ + theException.getMessage());
          theException.printStackTrace();
          System.exit(1);
        }
        for (short theIndex = 0; 
             theIndex < theComponents.length; theIndex++) 
               getComponent(theIndex).setBackground(getBackground());
        if (fButton.getLabel() == “White”)
          fButton.setBackground(java.awt.Color.white);
        else
          fButton.setBackground(java.awt.Color.red);
      }
    });

We handle the button click in three steps:

1.  Change the Container’s background and the label of the button to reflect the Container’s new state.
2.  Step through each Component in the Container, setting its background to match the background of the Container.
3.  Because step 2 changed the background of the button, change it back to match the button’s label.

Understanding fCheckbox and fDialog The Checkbox, named fCheckbox, is added in the same way as the other components we’ve seen, but then we do something different—we make a new Dialog. Our plan is to have a modal dialog box come up whenever the end user checks the Checkbox, and we have to go through a couple of extra steps to make this work.


NOTE:  Unlike Button and TextField, a Checkbox doesn’t send an ActionEvent. Instead, it sends an ItemEvent. The principle is the same as the ActionEvent, however—we just instantiate a new ItemListener.

If you examine the documentation for class Dialog, you’ll see that all its constructors need an owner—a Frame or another Dialog that serves as the parent of the new Dialog in the Window hierarchy. If you’re writing a pure application, you can use the application’s Frame as the owner of the Dialog. In this case, we can be called as either an applet or an application. An Applet is a Panel, not a Frame, so we have to start up our own Frame to serve as the parent.

At the bottom of the HelloPlus.java listing you’ll see a definition of class OKDialog. After the constructor (which is private) you’ll see a factory method: makeDialog():

static public OKDialog makeDialog(String theMessage) {
    if (fFrame == null)
      fFrame = new Frame();
    OKDialog theResult = new OKDialog(fFrame, theMessage);
    fFrame.setSize(theResult.getSize().width,
                   theResult.getSize().height);
    return theResult;
  }
} // end OKDialog class


NOTE:  A factory method returns a new object based on an existing object. For example, java.awt.geom.Arc2D supports makeBounds(), a factory method that returns a new Rectangle2D based on the bounding rectangle of the arc.

The class OKDialog has a static Frame member fFrame. When makeDialog() is first called, it instantiates a new Frame and assigns it to fFrame. Next, makeDialog() instantiates a new OKDialog, using fFrame as the parent. It scales the Frame to the same size as the OKDialog and returns a reference to the OKDialog for use by the calling class.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.