14

I was wondering how it would be possible to create my own listener for something, and to be able to use it just like any other listener, ex.

public interface Listener {

    public void onListen(Event e);

}

And then this in my class

public class test implements Listener {

    Object.add(new Listener() { 

        @Override
        public void onListen(Event e) {

        }
    });
}

I guess what i'm really asking also is where do I define how to check if something happens pertaining to the listener that I create?

If any of this doesn't make sense, please tell me and I will clarify it.

If at best this makes no sense to you, i'll try this. How would I make a listener to check if the selected option in a combobox has changed to a different option. I don't actually need a combobox listener though, that was just an example.

Charles
  • 50,943
  • 13
  • 104
  • 142
Pjrat111
  • 213
  • 1
  • 3
  • 9
  • 1
    Indeed, your question doesn't make sense yet. Are you asking how to register your listener with something? Or how to invoke the listener at the relevant time? Or something else? – Oliver Charlesworth Apr 29 '12 at 19:24
  • How I would create a custom listener to, say, check if a ComboBox option was changed? – Pjrat111 Apr 29 '12 at 19:27
  • 2
    @Pjrat111: Your particular example is explained in the Java Swing tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners. – Oliver Charlesworth Apr 29 '12 at 19:28
  • 1
    Java UI components already use this model, and there are appropriate interfaces to implement to achieve what you are asking. The tutorials explain how to use them. If you're asking how that actually *works*, the [Observer Pattern](http://en.wikipedia.org/wiki/Observer_pattern) is a good place to start. – Brian Roach Apr 29 '12 at 19:30
  • 1
    I realize this, but I do not actually need a listener for a combobox, or for any swing component, I'm just trying to learn how listeners work. Thank you for the observer pattern link @Brian Roach – Pjrat111 Apr 29 '12 at 19:34
  • @Pjrat111 - Also see the Java classes http://docs.oracle.com/javase/6/docs/api/java/util/Observer.html and http://docs.oracle.com/javase/6/docs/api/java/util/Observable.html – Brian Roach Apr 29 '12 at 19:36
  • @Pjrat111: Then it's still not clear what you're asking. – Oliver Charlesworth Apr 29 '12 at 19:37

2 Answers2

17

Here is a short example:

    private static class Position {
    }

    private static class Ball {
        private ArrayList<FootballObserver> observers = new ArrayList<FootballObserver>();
        private Position ballPosition = new Position();

        public void registerObserver(FootballObserver observer) {
            observers.add(observer);
        }

        public void notifyListeners() {
            for(FootballObserver observer : observers) {
                observer.notify(ballPosition);
            }
        }

        public void doSomethingWithFootballPosition() {
            //bounce etc
            notifyListeners();
        }
    }

    private static interface FootballObserver {
        void notify(Position ball);
    }

    private static class Player implements FootballObserver {
        public void notify(Position ball) {
            System.out.println("received new ball position");
        }
    }

    public static void main(String... args) {
        FootballObserver player1 = new Player();
        Ball football = new Ball();
        football.registerObserver(player1);
        football.doSomethingWithFootballPosition();
    }
Sebastian
  • 1,932
  • 1
  • 16
  • 15
  • 3
    See also [`EventListenerList`](http://docs.oracle.com/javase/6/docs/api/javax/swing/event/EventListenerList.html). – trashgod Apr 30 '12 at 01:07
  • 1
    @trashgod thank you for the tip. It was not easy to understand. I found this helpful: http://www.java2s.com/Tutorial/Java/0260__Swing-Event/ManagingListenerListswithEventListenerList.htm. – qben Mar 02 '13 at 21:22
1

There is no trouble creating the listeners that you need, but you only can apply it to classes that expect it (so you cannot do (new Object()).addMyListener)

You will need to add to the classes where you want to use the listener an addMyListener(MyListener myListener) method. It just stores the listeners that you pass to it in a list for later user. It would be a good idea creating an interface with the addMyListener method (and a fireMyListeners()) method too.

Of course, you must also provide the code to call the fireMyListeners() method, too. Then the fireMyListeners() will just loop through the listeners and call theirs notification method.

SJuan76
  • 24,532
  • 6
  • 47
  • 87