In this section you'll walk through four versions of a modest applet
containing two AWT components. A ChoiceApplet class is
adapted from old style AWT events to the delegation event
model. You should obtain a good sense of the way Beans can use
reflection, introspection, event handling,
and inner classes. You'll also see interesting uses of Class
objects and Method objects combined with reflection to perform
indirect method invocation.
At first, hooking event sources to event listeners that call event handlers seems a little mystical and complicated. This section should clarify what goes on behind the scenes in builder tools like BeanBox when you connect event sources to event targets.
You'll get back to some more interesting modifications of
NervousText in a little bit. These modifications will
make much more sense after you work through this section, which shows
how reflection works programatically. In other words, you'll see how
to write programs that use introspection and reflection that do not
require builder tools to run and test. Here's what you'll see in the
next four sections:
The ChoiceApplets (versions 1-4) should give you a good introduction to the way Beans make use of reflection, introspection, event handling, and inner classes. The applets will also demonstrate differences in event handling between JDK TM 1.0 and JDK 1.1.
This section introduces ChoiceApplet01, the foundation for changes you will make in the four following sections. The ChoiceApplet starts with a simple Choice AWT component and adds a TextField. ChoiceApplet01 is built on the JDK 1.0 event model and provides a good example of what you'll need to do if you want to upgrade AWT programs to take advantage of the delegation event model introduced in JDK 1.1.
In this section you'll see a mixture of JDK 1.0 and JDK 1.1 event handling. The delegation event model is introduced. You'll see howTextFieldandChoicecomponents can be reqested to notifiy interesed listeners about events. You'll also see how to define an ActionListener inner class to handle events generated by these components. As an exercise, you can adapt the existing version of ChoiceApplet01 into a new version called ChoiceApplet02.
Using the ActionListener event handler for the TextField as an model, you can now convert event handling code for the Choice component. A general procedure for converting code is referenced and the changes in this section provide a concrete example for converting programs to use JDK 1.1 AWT events. The work here is done in preparation for using reflection for method invocation based on user input to be demonstrated in the next section. As an exercise, you can adapt the existing version of ChoiceApplet02 into a new version called ChoiceApplet03.
The Beans introspection API is built on top of reflection mechanisms introduced with JDK 1.1 This section shows how to invoke a method whose name has been typed into a TextField component. Though the example is artificial, it does give a sense of how to retrieve and invokeMethodsfromClassobjects based on method signatures. As an exercise, you can adapt the existing version of ChoiceApplet03 into a new version called ChoiceApplet04.
For information about changes to the java.awt packages, see
LINKTEXT GUI Changes: The AWT Grows Up
LINKFILE $TOP/post1.0/ui/index.html
)
.
While it is possible to build and use transitional Beans with JDK 1.0.2 and later versions of the JDK, you'll want to focus on using the new AWT classes to take full advantage of JavaBeansTM. The only reason to build transitional Beans is if your code must run in a browser that does yet support JDK 1.1 Java code.
You'll find some outstanding documents explaining how to upgrade you AWT component code to take advantage of the new event model. The new event model is one of the most significant changes introduced with JDK 1.1.
In this section, you'll see an example converted to use the JDK 1.1 event model. All AWT components are implemented as Beans, so you'll want to learn how to make effective use of the event model, so you can build Beans based upon the components.
A makefile for this lesson automates source code compilation.
|
|
|||
|
|
|
Questions? 30-Jan-98 Copyright © 1996,1997 Sun Microsystems Inc. All Rights Reserved. |
|