|
Préférences
Moteurs de recherche
|
|||||||||||||||||||||||||||
JavaTM 2 Platform Std. Ed. v1.6.0
java.beans
|
Constructor Summary | |
---|---|
EventHandler(Object target,
String action,
String eventPropertyName,
String listenerMethodName)
Creates a new EventHandler object;
you generally use one of the create methods
instead of invoking this constructor directly. |
Method Summary | ||
---|---|---|
static
|
create(Class<T> listenerInterface,
Object target,
String action)
Creates an implementation of listenerInterface in which
all of the methods in the listener interface apply
the handler's action to the target . |
|
static
|
create(Class<T> listenerInterface,
Object target,
String action,
String eventPropertyName)
/** Creates an implementation of listenerInterface in which
all of the methods pass the value of the event
expression, eventPropertyName , to the final method in the
statement, action , which is applied to the target . |
|
static
|
create(Class<T> listenerInterface,
Object target,
String action,
String eventPropertyName,
String listenerMethodName)
Creates an implementation of listenerInterface in which
the method named listenerMethodName
passes the value of the event expression, eventPropertyName ,
to the final method in the statement, action , which
is applied to the target . |
|
String |
getAction()
Returns the name of the target's writable property that this event handler will set, or the name of the method that this event handler will invoke on the target. |
|
String |
getEventPropertyName()
Returns the property of the event that should be used in the action applied to the target. |
|
String |
getListenerMethodName()
Returns the name of the method that will trigger the action. |
|
Object |
getTarget()
Returns the object to which this event handler will send a message. |
|
Object |
invoke(Object proxy,
Method method,
Object[] arguments)
Extract the appropriate property value from the event and pass it to the action associated with this EventHandler . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EventHandler(Object target, String action, String eventPropertyName, String listenerMethodName)
EventHandler
object;
you generally use one of the create
methods
instead of invoking this constructor directly. Refer to
the general version of create
for a complete description of
the eventPropertyName
and listenerMethodName
parameter.
target
- the object that will perform the actionaction
- the name of a (possibly qualified) property or method on
the targeteventPropertyName
- the (possibly qualified) name of a readable property of the incoming eventlistenerMethodName
- the name of the method in the listener interface that should trigger the action
NullPointerException
- if target
is null
NullPointerException
- if action
is nullEventHandler
,
create(Class, Object, String, String, String)
,
getTarget()
,
getAction()
,
getEventPropertyName()
,
getListenerMethodName()
Method Detail |
---|
public Object getTarget()
EventHandler(Object, String, String, String)
public String getAction()
EventHandler(Object, String, String, String)
public String getEventPropertyName()
EventHandler(Object, String, String, String)
public String getListenerMethodName()
null
signifies that all methods in the
listener interface trigger the action.
EventHandler(Object, String, String, String)
public Object invoke(Object proxy, Method method, Object[] arguments)
EventHandler
.
invoke
in interface InvocationHandler
proxy
- the proxy objectmethod
- the method in the listener interfacearguments
- an array of objects containing the values of the
arguments passed in the method invocation on the proxy instance,
or null
if interface method takes no arguments.
Arguments of primitive types are wrapped in instances of the
appropriate primitive wrapper class, such as
java.lang.Integer
or java.lang.Boolean
.
EventHandler
public static <T> T create(Class<T> listenerInterface, Object target, String action)
listenerInterface
in which
all of the methods in the listener interface apply
the handler's action
to the target
. This
method is implemented by calling the other, more general,
implementation of the create
method with both
the eventPropertyName
and the listenerMethodName
taking the value null
. Refer to
the general version of create
for a complete description of
the action
parameter.
To create an ActionListener
that shows a
JDialog
with dialog.show()
,
one can write:
EventHandler.create(ActionListener.class, dialog, "show")
listenerInterface
- the listener interface to create a proxy fortarget
- the object that will perform the actionaction
- the name of a (possibly qualified) property or method on
the target
listenerInterface
NullPointerException
- if listenerInterface
is null
NullPointerException
- if target
is null
NullPointerException
- if action
is nullcreate(Class, Object, String, String)
public static <T> T create(Class<T> listenerInterface, Object target, String action, String eventPropertyName)
listenerInterface
in which
all of the methods pass the value of the event
expression, eventPropertyName
, to the final method in the
statement, action
, which is applied to the target
.
This method is implemented by calling the
more general, implementation of the create
method with
the listenerMethodName
taking the value null
.
Refer to
the general version of create
for a complete description of
the action
and eventPropertyName
parameters.
To create an ActionListener
that sets the
the text of a JLabel
to the text value of
the JTextField
source of the incoming event,
you can use the following code:
This is equivalent to the following code:EventHandler.create(ActionListener.class, label, "text", "source.text");
//Equivalent code using an inner class instead of EventHandler. new ActionListener() { public void actionPerformed(ActionEvent event) { label.setText(((JTextField)(event.getSource())).getText()); } };
listenerInterface
- the listener interface to create a proxy fortarget
- the object that will perform the actionaction
- the name of a (possibly qualified) property or method on
the targeteventPropertyName
- the (possibly qualified) name of a readable property of the incoming event
listenerInterface
NullPointerException
- if listenerInterface
is null
NullPointerException
- if target
is null
NullPointerException
- if action
is nullcreate(Class, Object, String, String, String)
public static <T> T create(Class<T> listenerInterface, Object target, String action, String eventPropertyName, String listenerMethodName)
listenerInterface
in which
the method named listenerMethodName
passes the value of the event expression, eventPropertyName
,
to the final method in the statement, action
, which
is applied to the target
. All of the other listener
methods do nothing.
The eventPropertyName
string is used to extract a value
from the incoming event object that is passed to the target
method. The common case is the target method takes no arguments, in
which case a value of null should be used for the
eventPropertyName
. Alternatively if you want
the incoming event object passed directly to the target method use
the empty string.
The format of the eventPropertyName
string is a sequence of
methods or properties where each method or
property is applied to the value returned by the preceeding method
starting from the incoming event object.
The syntax is: propertyName{.propertyName}*
where propertyName
matches a method or
property. For example, to extract the point
property from a MouseEvent
, you could use either
"point"
or "getPoint"
as the
eventPropertyName
. To extract the "text" property from
a MouseEvent
with a JLabel
source use any
of the following as eventPropertyName
:
"source.text"
,
"getSource.text"
"getSource.getText"
or
"source.getText"
. If a method can not be found, or an
exception is generated as part of invoking a method a
RuntimeException
will be thrown at dispatch time. For
example, if the incoming event object is null, and
eventPropertyName
is non-null and not empty, a
RuntimeException
will be thrown.
The action
argument is of the same format as the
eventPropertyName
argument where the last property name
identifies either a method name or writable property.
If the listenerMethodName
is null
all methods in the interface trigger the action
to be
executed on the target
.
For example, to create a MouseListener
that sets the target
object's origin
property to the incoming MouseEvent
's
location (that's the value of mouseEvent.getPoint()
) each
time a mouse button is pressed, one would write:
This is comparable to writing aEventHandler.create(MouseListener.class, "mousePressed", target, "origin", "point");
MouseListener
in which all
of the methods except mousePressed
are no-ops:
//Equivalent code using an inner class instead of EventHandler. new MouseAdapter() { public void mousePressed(MouseEvent e) { target.setOrigin(e.getPoint()); } };
listenerInterface
- the listener interface to create a proxy fortarget
- the object that will perform the actionaction
- the name of a (possibly qualified) property or method on
the targeteventPropertyName
- the (possibly qualified) name of a readable property of the incoming eventlistenerMethodName
- the name of the method in the listener interface that should trigger the action
listenerInterface
NullPointerException
- if listenerInterface
is null
NullPointerException
- if target
is null
NullPointerException
- if action
is nullEventHandler