How do event listeners recognize specific events in each of their methods?

I'm having trouble understanding exactly how a method like

public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent e) { //code for what happens when you click the mouse on the component } } 

knows the identifier of the mouse event. Why is the event only dispatched to the mouseClicked method when the event is executed? Constructors for these methods require any of the possible mouse events, so why is it sent only to the mouseClicked method when other methods have the same constructor (i.e. MousePressed, mouseReleased, etc.).

+4
source share
3 answers

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); // invoking a specific listener method break; case MouseEvent.MOUSE_RELEASED: listener.mouseReleased(e); break; ... } 
+3
source

The most important information is to know the observation pattern .

This template is used to form relationships between objects at runtime.

The idea of โ€‹โ€‹the template is simple - one of the more Observers interested in the Subject state and register their interest in the Subject by joining themselves. When something changes in our Subject, which the Observer may be interested in, a notification is sent that calls the update method in each observer. When the Observer is no longer interested in the state of the Subject, they can simply separate themselves.

The whole idea of โ€‹โ€‹action listeners is based on this. Once you understand this pattern, it's simple.

When you register an ActionListener, the observer and button is the theme. Therefore, when the button changes state, the actionPerformed method is launched.

Please note that all listeners are based on this template, when an event occurs, registered ones will be notified of this event and will perform actions. For example, Swing manages the entire notification of registration and the event itself (it is already "built-in").

( http://java.dzone.com/articles/design-patterns-uncovered )

+5
source

When you register for an event, you pass the listener the object from which you want to hear. This object stores a list of objects of interest, and if something happens that requires notification, iterates through the list and calls the notification method.

-1
source

Source: https://habr.com/ru/post/1480578/


All Articles