Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am using Java.

How does an event source like a (button in a GUI) knows which method to call in an interface?

For example, I am listening to Mouse Events and have implemented a Mouse Listener interface in my class then how would the event source know whether it should call mousePressed(), mouseReleased() or some other abstract method in the in the interface?

I am aware that using addMouseListener(this), provides a way for the event source to call the method of an interface.

share|improve this question
    
It depends on the OS/language you're using. In very general terms, however, you're implementing the observer design pattern. However its done in your particular language your GUI class (the publisher) has a method that is associated with the actual click. The actual click causes that method to run. Then that method calls the "right click" method of the subscriber (i.e. the mouse listener interface). So in a sense this is a two-level publish/subscribe as it were. –  radarbob Jul 23 '12 at 16:28
    
@radarbob, i'm not clear about the concept that you described previously. Could you please explain it a bit more clearly. –  Ankit Jul 24 '12 at 13:49

1 Answer 1

For the observable design pattern, see http://en.wikipedia.org/wiki/Observer_pattern

Roughly, when the user clicks the button of the mouse and your Java Application is active, the OS notifies the application (in this case, the JVM) about the click. Then the JVM creates the MouseEvent object, as well as other necessary objects for handling the event and searches for the listeners of that event (think that the Button object has a collection of MouseListener objects, and you added your listener to that collection). Then it iterates through all the listeners in that collection and calls the appropriate method. It knows what is the appropriate method because it's defined in the MouseListener interface, thus it knows all objects in that collection implements the methods defined in the interface.

For example, suppose there's this code in a Button class (it's probably somewhere else, but it serves as an example):

// suppose mouseListenerCollection contains all the mouse listeners in the component
for(MouseListener listener : mouseListenerCollection){
    listener.mouseClicked(event);
}

It invokes the mouseClicked(MouseEvent e) method because the mouse have been clicked and all objects that implements the MouseListener interface implements the mouseClicked method, and it's specified that when a mouse is clicked, the mouseClicked method must be called.

For further information, always refer to the API javadoc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.