|
Now you'll see how to invoke a method whose
name has been typed into a
You're now going to add two methods
which will be invoked by using
reflection. Given a string
naming a method, a import java.lang.reflect.*; Now have a look at the methods
that will be called based on
First add a
public void paint(Graphics g) {
g.drawRect(x,y,w,h);
}
Instance variables should be declared
at the top of the class definition
to control the rectangle's size:
The paint method will be called
automatically to update the applets
frame with the newly drawn rectangle
whenever a repaint method is called.
This is done inside two new methods
that you must now define:
The first draws a big rectangle
the second draws a small one.
You will not be calling these
methods directly, but indirectly
throught the Reflect lookup
mechansism. This is the essence
of last four sections of exercises.
The method which does the lookup
and invocation is called
Given a String, mName
which names the method,
the Class object for
ChoiceApplet04 can call
getDeclaredMethod
to get a Method object, m.
To get the applet's class, use:
ChoiceApplet04.class.
The
getDeclaredMethod
gets two arguments: a String,
naming the method for the
class, and an array of argments.
Essentially you are
the signature of
the desired method in the
call
ChoiceApplet04.class.getDeclaredMethod(mName, null);Since no arguments are required by either the big or small methods,
null is given as the second argument.
Now that you have the desired
m.invoke(this, null);
If either the method lookup or the invocation
fails, an exception is thrown in the
try block. If this
happens, the exception handling code
in the catch block reports that
"no such method:" exists and prints
the offending method name.
Method names "big" or "small" will
work; any other names will throw
an exception.
Just for tidiness, you'll want to
keep the text displayed in
the
Immediatley after this code, add a statment to call invoke based on the String now in the TextField:
invoke(mName);
The same call must be added to the
event handler code for the Choice
component.
The respective listener registration
and event handler definitions look
like this:
Hopefully that wasn't too bad. Once you get the hang of inner classes, events and event listeners, you'll be on solid ground for programming JDK 1.1 applications and applets. Reflection is worth knowing about in certain situations. As mentioned if your are building programming environments and tools that need to find out the capabilities of classes at run time, you'll find Reflection is indespensible. For typical programs, however, you should focus on the new event model.
You'll find more detailed examples of event handlers, inner classes, reflection, and introspection in the next four sections starting with#usemacro BEANSLINK ( LINKFILE $NEXTFILE LINKTEXT Automatically Resized Beans ) You can use TestChoiceApplet04.html to test the applet. Program Source Code A makefile for this lesson automates source code compilation, JAR file construction, and copying of JAR files to the appropriate BeanBox directory. You'll have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and BDK installation directories. You may want to look at the final source file for ChoiceApplet04.java to verify changes. | ||||||||||
|
| ||||||||||||