David Petersheim [www.code316.com] gave a talk at our local JUG in Louisville, Kentucky [of which he’s the founder] on Reflection. He’d been messing around with using reflection to get rid of the pain of anonymous Listeners and back to the VB he loves so well [insulting dig]. Jim Birchfield suggested some improvements on David’s approach and I took these and mixed them with some bits I wanted there to get ProxyListener [probably not an original idea I’m sure].
Anyway, ProxyListener allows you to implement Listeners with the following code snippet:
button.addActionListener( (ActionListener)ProxyListener.newInstance(
ActionListener.class, this, “flipColor”
) );
and then have a method of:
public void flipColor() { …
or
public void flipColor(ActionEvent ae) { …
[or a couple of other variants. It’s not fussy.]
The basic point being that a lot of listeners are merely bouncers off to another method somewhere, so why not make life a little easier. It needs more overloading of the newInstance methods, and it would be nice to be able to pass in a Class as a target rather than just an Object [this in the example above].
Take a look at ProxyListener. Take note that it’s JDK 1.3 and above. It’s a natural complement to an early class/blog on Notifier.