Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Java Beans Tutorial, Part 4
Reflection and Introspection

By Greg Voss, JavaSoft

[Tutorial Contents]

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:

Event Handling, Inner Classes, Reflection, Introspection, and Method Invocation

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 JDKTM 1.0 and JDK 1.1.

New and Old Event Models ChoiceApplet01

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.

ActionListeners for Handling Events ChoiceApplet02

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 how TextField and Choice components 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.

Upgrading Event Handlers ChoiceApplet03

Using the ActionListener event handler for the TextField as a 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.

Invoking Reflected Methods ChoiceApplet04

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 invoke Methods from Class objects based on method signatures. As an exercise, you can adapt the existing version of ChoiceApplet03 into a new version called ChoiceApplet04.

Upgrading AWT code for JDK 1.1

For information about changes to the java.awt packages, see GUI Changes: The AWT Grows Up .

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.


Program Source Code

You'll find references to all four versions of the Choice applet covered in the next four sections. You'll also find HTML code that can be used to run the applet. The JDK appletviewer will work as well. You don't need to read these now; they are consolidated here for convenience. Each of the subsequent sections will provide links to relevant example programs.

A makefile for this lesson automates source code compilation.


ChoiceApplet Example Programs

ChoiceApplet01.java

ChoiceApplet02.java

ChoiceApplet03.java

ChoiceApplet04.java


[ This page was updated: 5-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.