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


Most of these components provide familiar functionality. A Button, for example, works the way you expect buttons to work in any GUI. The classes Canvas and Composite enable you to use drawing primitives to build a custom GUI component. What’s most significant about this list is that it includes java.awt.Container, which means you can build a new drawable area and add it to an existing Container. The AWT supplies a variety of subclasses of Container, including Panel, ScrollPane, and Window.

Window, in turn, has two subclasses in the AWT: Dialog and Frame. (Each of those classes has further subclasses, but we’re not concerned about those just now.) To add a GUI to an application, you must supply a top-level Frame into which the application can add Components. That’s what we’re doing in line 18 of Listing 38.1:

class HelloApplicationFrame extends Frame


NOTE:  If you run HelloApplication as an application, you invoke main() which starts by making a new HelloApplicationFrame, and then transfers control to it. If you load HelloApplication as an applet, the browser and the Java runtime work together to make a new frame inside the browser window. Then the Java runtime calls the applet’s init(), start(), and paint() methods.

Introducing the JDK 1.1 Delegation Event Model

Much of the work of writing a user interface has to do with communication. If the operating system detects a key being pressed or a mouse button coming up, it needs to notify the right application. The application, in turn, needs to find out which components are interested in that activity and send them a message. Starting with JDK 1.1, Sun introduced a new way of communicating information about events: the delegation event model. Prior to JDK 1.1 the model was, well, different. Don’t worry about how the user interface worked in JDK 1.0—it’s gone for good. Do be aware that the mechanism did change; if you have occasion to read old code or you have old books on Java, don’t copy the way they do things. Its use is discouraged in JDK 1.1 or later because it doesn’t support some of the newest features (such as Java’s component model, JavaBeans).


NOTE:  JavaBeans is an advanced topic not covered in this book. To learn more about Beans, see Chapters 19 and 20 of Using Java 1.2 (Que, 1998), or read Sams Teach Yourself JavaBeans in 21 Days (Sams, 1997).

So how does the delegation event model work? Every element of communication between the GUI and the program is defined as an event. Application classes register their interest in particular events from particular components by asking the component to add their listener to a list. When the event occurs, the event source notifies all registered listeners.

Two kinds of events exist: low-level events and semantic events. All of them are derived from java.awt.AWTEvent. A low-level event is concerned with the physical aspects of the user interface—mouse clicks, key presses, and the like. Semantic events are based on low-level events. To choose a menu item, for example, a user may click the menu bar, then click a menu item. To “click” means to press the mouse button down and then up again. This series of low-level events (mouse-down on the menu bar, mouse-up on the menu bar, mouse-down on the menu item, and mouse-up on the menu item) is combined into one semantic event.

Class ComponentEvent includes a special type of event that is not used with the delegation event model: PaintEvent. A PaintEvent signals that the operating system wants to redraw a portion of the user interface. A component must override paint() or update() to make sure it handles the PaintEvent correctly.

Look at line 23 of Listing 38.1:

addWindowListener(new HelloWindowAdapter());

This line is part of HelloApplicationFrame’s constructor. As the new Frame is being built, this line tells the Frame that it is interested in certain events. Rather than notify it directly, however, the constructor tells it to send the notifications to an adapter—a convenience class that handles only one kind of event. Our adapter is called HelloWindowAdapter—it’s an instance of WindowAdapter, which is interested in WindowEvents. When the frame sends a WindowEvent, HelloWindowAdapter looks to see if the message is windowClosing. If it isn’t, HelloWindowAdapter ignores it, but if it is, the program starts the process of shutting down.

Because HelloApplication is written as both an applet and an application, it begins its shutdown by calling the methods a browser calls when it wants to shut down an applet—stop() and destroy(). Finally it calls exit(), ending the application and enabling the Frame to close.

Class AWTEvent also includes semantic events:

  ActionEvent—Notifies your program about component-specific actions such as button clicks
  AdjustmentEvent—Tells you that a scrollbar has been adjusted
  ItemEvent—Notifies your program when the user interacts with a choice, list, or check box
  TextEvent—Tells you when the user changes text in a TextArea or TextField component


NOTE:  Many user actions, such as mouse clicks, send events at both the low level and the semantic level. If you move your mouse onto a button and click the mouse button, you’ll get a series of MouseEvents: one for when the mouse enters the component, one for the button press, one for the button release, and one for the click itself. You’ll also get a single ActionEvent from the button saying that it was clicked. Unless you need fine-grained control of the user interface, listen for semantic events.

Drawing and adding—Constructing the User Interface

In Listing 38.1 we implemented the applet’s paint method by calling drawString() on the Graphics object. The drawString() method is one of the drawing primitives that has been available in Java from its earliest days. Graphics’ drawing methods also include

  draw3DRect()
  drawArc()
  drawBytes()
  drawChars()
  drawImage()
  drawLine()
  drawOval()
  drawPolygon()
  drawPolyline()
  drawRect()
  drawRoundRect()


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.