EventHandler is to extract a property value from the first argument of the method in the listener interface (typically an event object) and use it to set the value of a property in the target object. In the following example we create an ActionListener that sets the nextFocusableComponent property of the target (myButton) object to the value of the "source" property of the event. This would correspond to the following inner class implementation:EventHandler.create(ActionListener.class, myButton, "nextFocusableComponent", "source")
//Equivalent code using an inner class instead of EventHandler. new ActionListener() { public void actionPerformed(ActionEvent e) { myButton.setNextFocusableComponent((Component)e.getSource()); } } It's also possible to create an EventHandler that just passes the incoming event object to the target's action. If the fourth EventHandler.create argument is an empty string, then the event is just passed along: This would correspond to the following inner class implementation:EventHandler.create(ActionListener.class, target, "doActionEvent", "")
//Equivalent code using an inner class instead of EventHandler. new ActionListener() { public void actionPerformed(ActionEvent e) { target.doActionEvent(e); } } Probably the most common use of EventHandler is to extract a property value from the source of the event object and set this value as the value of a property of the target object. In the following example we create an ActionListener that sets the "label" property of the target object to the value of the "text" property of the source (the value of the "source" property) of the event. This would correspond to the following inner class implementation:EventHandler.create(ActionListener.class, myButton, "label", "source.text")
//Equivalent code using an inner class instead of EventHandler. new ActionListener { public void actionPerformed(ActionEvent e) { myButton.setLabel(((JTextField)e.getSource()).getText()); } } The event property may be "qualified" with an arbitrary number of property prefixes delimited with the "." character. The "qualifying" names that appear before the "." characters are taken as the names of properties that should be applied, left-most first, to the event object. For example, the following action listener
might be written as the following inner class (assuming all the properties had canonical getter methods and returned the appropriate types):EventHandler.create(ActionListener.class, target, "a", "b.c.d")
//Equivalent code using an inner class instead of EventHandler. new ActionListener { public void actionPerformed(ActionEvent e) { target.setA(e.getB().getC().isD()); } } The target property may also be "qualified" with an arbitrary number of property prefixs delimited with the "." character. For example, the following action listener: EventHandler.create(ActionListener.class, target, "a.b", "c.d")might be written as the following inner class (assuming all the properties had canonical getter methods and returned the appropriate types):
//Equivalent code using an inner class instead of EventHandler. new ActionListener { public void actionPerformed(ActionEvent e) { target.getA().setB(e.getC().isD()); } } As EventHandler ultimately relies on reflection to invoke a method we recommend against targeting an overloaded method. For example, if the target is an instance of the class MyTarget which is defined as:
public class MyTarget { public void doIt(String); public void doIt(Object); } Then the method doIt is overloaded. EventHandler will invoke the method that is appropriate based on the source. If the source is null, then either method is appropriate and the one that is invoked is undefined. For that reason we recommend against targeting overloaded methods.
@see java.lang.reflect.Proxy
@see java.util.EventObject
@since 1.4
@author Mark Davidson
@author Philip Milne
@author Hans Muller
@version 1.21, 05/23/06
| |
| |