public class EventListenerProxy extends Object implements InvocationHandler
final JButton apply = new JButton("Apply");
apply.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
apply.setEnabled(false);
Worker.post(new Job()
{
public Object run()
{
// Lenghty apply code
}
});
}
}));
JButton cancel = new JButton("Cancel");
cancel.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// For example, dispose a dialog
}
}));
Without using EventListenerProxy, when a user clicks on the apply button and immediately
after on the cancel button, it happens that apply's button listener is executed; in there,
usage of Foxtrot's Worker will dequeue and execute the event associated to the cancel
button click, that will - for example - dispose the dialog before the
apply operation is finished.
button.addActionListener(new ActionListener() {...});
to this:
button.addActionListener((ActionListener)EventListenerProxy.create(ActionListener.class, new ActionListener() {...}));
| Modifier | Constructor and Description |
|---|---|
protected |
EventListenerProxy(EventListener listener)
Creates an instance that wraps the given listener
|
| Modifier and Type | Method and Description |
|---|---|
static EventListener |
create(Class listenerInterface,
EventListener listener)
Creates a proxy for the given listener.
|
Object |
invoke(Object proxy,
Method method,
Object[] args) |
protected EventListenerProxy(EventListener listener)
public static EventListener create(Class listenerInterface, EventListener listener)
listenerInterface - The interface used to create the proxylistener - The listener to proxyNullPointerException - When the interface or the listener is nullIllegalArgumentException - When the listener does not implement the interfaceCopyright © 2002-2011. All Rights Reserved.