myDoItButton.addActionListener( new GenericActionListener( this, "doit" ) );
myCloseButton.addActionListener( new GenericActionListener( that, "close" ) );
public class GenericActionListener implements ActionListener
{
String methodName;
Object target;
/**
* constructor
* @param target object whose method is to be called when
* the event happens.
* @param methodName name of parameterless method on target
* to be called when the event happens.
*/
public GenericActionListener ( Object target, String methodName )
{
this.target = target;
this.methodName = methodName;
}
/**
* @inheritDoc
*/
public void actionPerformed ( ActionEvent e )
{
try
{
Class[] argtypes = {};
Method method = target.getClass().getMethod( methodNeme, argTypes );
Object args = {};
method.invoke( target, args );
}
catch ( Exception ex )
{
ex.printStackTrace();
throw new IllegalArgumentException(
"GenericActionListener: no such method "
+ methodName );
}
}
}