public abstract class GenericListener
extends java.lang.Object
Usage:
public class Demo {
JPanel root = new JPanel(new BorderLayout());
JLabel label = new JLabel(" ");
public void myButtonAction(ActionEvent e) {
label.setText("buttonAction");
}
public void myMouseEntered(MouseEvent e) {
label.setText("mouseEntered: "+e.toString());
}
Demo() {
JButton button = new JButton("Button with Dynamic Listener");
//This listener will be generated at run-time, for example at run-time
// an ActionListener class will be code-generated and then
// class-loaded. Only one of these is actually created, even
// if many calls to GenericListener.create(ActionListener.class ...)
// are made.
ActionListener actionListener = (ActionListener)(GenericListener.create(
ActionListener.class,
"actionPerformed",
this,
"myButtonAction")
);
button.addActionListener(actionListener);
// Here's another dynamically generated listener. This one is
// a little different because the listenerMethod argument actually
// specifies one of many listener methods. In the previous example
// "actionPerformed" named the one and only ActionListener method.
MouseListener mouseListener = (MouseListener)(GenericListener.create(
MouseListener.class,
"mouseEntered",
this,
"myMouseEntered")
);
button.addMouseListener(mouseListener);
| Modifier and Type | Method and Description |
|---|---|
static java.lang.Object |
create(java.lang.Class listenerInterface,
java.lang.String listenerMethodName,
java.lang.Object target,
java.lang.String targetMethodName)
A convenient version of
create(listenerMethod, targetObject, targetMethod). |
static java.lang.Object |
create(java.lang.reflect.Method listenerMethod,
java.lang.Object target,
java.lang.reflect.Method targetMethod)
Return an instance of a class that implements the interface that contains
the declaration for
listenerMethod. |
public static java.lang.Object create(java.lang.Class listenerInterface,
java.lang.String listenerMethodName,
java.lang.Object target,
java.lang.String targetMethodName)
create(listenerMethod, targetObject, targetMethod).
This version looks up the listener and target Methods, so you don't have to.public static java.lang.Object create(java.lang.reflect.Method listenerMethod,
java.lang.Object target,
java.lang.reflect.Method targetMethod)
listenerMethod. In this new class,
listenerMethod will apply target.targetMethod
to the incoming Event.