Such an interface begins to be useful when you implement it, create an object, and register this object in the event source. The registration part is important here - if you register on mouse click events, then what the object will receive.
Thus, basically these interfaces look the same, because they are used to handle similar events, but in the end you will be informed only about registered events. There is no magic here - the source of events inside stores a collection of listeners who are interested in an event, and if such an event occurs, it iterates through the collection and calls the listener method.
For example, the mouse listener interface you mentioned has several methods:
public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); ... }
if you look at part of the java.awt.Component class, you will see that it is an event source that takes care of the correct method:
public abstract class Component implements ImageObserver, MenuContainer, Serializable { ... protected void processMouseEvent(MouseEvent e) { MouseListener listener = mouseListener; if (listener != null) { int id = e.getID(); switch(id) { case MouseEvent.MOUSE_PRESSED: listener.mousePressed(e);
source share