Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Listeners in Java

Tags:

java

listener

I've been working with event listeners in AS3, but seems like there is none in java (except for graphics components). It's surprising.

The question is, how could i implement my own event listener in java? Maybe someone did that work before?

like image 303
Wlofrevo Kcast Avatar asked Dec 21 '12 13:12

Wlofrevo Kcast


People also ask

What are the event listeners?

An event listener is a procedure in JavaScript that waits for an event to occur. The simple example of an event is a user clicking the mouse or pressing a key on the keyboard.

What is event listener and its classes?

The Event listener represent the interfaces responsible to handle events. Java provides us various Event listener classes but we will discuss those which are more frequently used. Every method of an event listener method has a single argument as an object which is subclass of EventObject class.

What is meant by event listener interface in Java?

The EventListener interface is the primary method for handling events. Users implement the EventListener interface and register their listener on an EventTarget using the AddEventListener method. The users should also remove their EventListener from its EventTarget after they have completed using the listener.


4 Answers

You can define a Listener interface:

public interface EventListener {
    void fireEvent (Event e);
}

Then in your code:

EventListener lst = new EventListener() {
    @Override
    public void fireEvent (Event e) {
        //do what you want with e
    }
}

someObject.setListener(lst);
someObject.somethingHappened();

Then in someObject (in practice you would probably hold a list of listeners):

public class SomeObject {
    private EventListener lst;

    public void setListener (EventListener lst) {
        this.lst = lst;
    }

    public void somethingHappened () {
        lst.fireEvent(new Event("Something Happened"));
    }
}
like image 149
assylias Avatar answered Oct 22 '22 06:10

assylias


You can use PropertyChangeSupport with PropertyChangeListener or use Observer pattern.

like image 39
mleczey Avatar answered Oct 22 '22 07:10

mleczey


First of all you need some source of events, so you can attache listener to it. If you need custom listener then you need also implement the custom source.

In Java you can find existing sources and listener interfaces. As you mentioned GUI is usually based on events. If you are into 3D then rendering engines deliver appropriate API (eg. collision detection), file system hooks, properties change listeners (Android).

It depends what are your needs. For most usages there should be already a library that delivers you appropriate API.

While implementing your own solution then for application wide event handling the Event Bus might be a good choice. My preferred implementation is in Guava library: http://code.google.com/p/guava-libraries/wiki/EventBusExplained

like image 30
Aleksander Gralak Avatar answered Oct 22 '22 07:10

Aleksander Gralak


You can implement kind of listeners in Java extending Observable class for the objects you want to observe, and on the listeners, you implement Observer.

like image 2
HericDenis Avatar answered Oct 22 '22 06:10

HericDenis