Created
January 15, 2017 18:02
-
-
Save petar-dambovaliev/7730493b4c1406d821e264eb160e4e15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.EventListener; | |
import java.util.EventObject; | |
import javax.swing.event.EventListenerList; | |
class MyEvent extends EventObject { | |
public MyEvent(Object source) { | |
super(source); | |
} | |
} | |
interface MyEventListener extends EventListener { | |
public void myEventOccurred(MyEvent evt); | |
} | |
class MyClass { | |
protected EventListenerList listenerList = new EventListenerList(); | |
public void addMyEventListener(MyEventListener listener) { | |
listenerList.add(MyEventListener.class, listener); | |
} | |
public void removeMyEventListener(MyEventListener listener) { | |
listenerList.remove(MyEventListener.class, listener); | |
} | |
void fireMyEvent(MyEvent evt) { | |
Object[] listeners = listenerList.getListenerList(); | |
for (int i = 0; i < listeners.length; i = i+2) { | |
if (listeners[i] == MyEventListener.class) { | |
((MyEventListener) listeners[i+1]).myEventOccurred(evt); | |
} | |
} | |
} | |
} | |
public class Main { | |
public static void main(String[] argv) throws Exception { | |
MyClass c = new MyClass(); | |
c.addMyEventListener(new MyEventListener() { | |
public void myEventOccurred(MyEvent evt) { | |
System.out.println("fired"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment