Custom event

S

Suma Shanbhog

Hello All,

How can I write a custom event?
I want to create a custom event object and fire it at a particular point in
program.

Thanks in advance!

Regards
Suma
 
V

VisionSet

Suma Shanbhog said:
Hello All,

How can I write a custom event?
I want to create a custom event object and fire it at a particular point in
program.

Thanks in advance!

public class MyEvent {}

public interface MyListener {
void eventFired(MyEvent evt);
}

public class AnObservable {
private List<MyListener> listeners;

public void fire() {
for (MyListener ml : listeners) ml.fire(new MyEvent());
}

public void addListener(MyListener ml) {
listener.add(ml);
}
}

public class AnObserver {
private MyListener myListener = new MyListener() {
public void eventFired(MyEvent evt) {
// react to event being fired
}
};
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top