/*
* ActionEventTest.java
*/
import java.awt.event.*;
import javax.swing.*;
public class ActionEventTest {
public ActionEventTest() {
MyComponent comp = new MyComponent();
comp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEventTest: " +
"MyComponent-actionPerformed: "+e.getActionCommand());
}
});
comp.doSomeThing();
}
public static void main(String args[]) {
new ActionEventTest();
}
}
class MyComponent extends JPanel{
public void doSomeThing(){
System.out.println("MyComponent: doSomeThing");
fireActionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, "OK"));
}
public void addActionListener(ActionListener l) {
listenerList.add(ActionListener.class, l);
}
protected void fireActionPerformed(ActionEvent event) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
ActionEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ActionListener.class) {
// Lazily create the event:
if (e == null) {
String actionCommand = event.getActionCommand();
e = new ActionEvent(MyComponent.this,
ActionEvent.ACTION_PERFORMED,
actionCommand,
event.getWhen(),
event.getModifiers());
}
((ActionListener)listeners[i+1]).actionPerformed(e);
}
}
}
}