0

consider this class:

public class mycomponent extends JComponent {
 public mycomponent(){
 addMouseMotionListener(new MouseMotionHandler());
 }
 class MouseMotionHandler implements MouseMotionListener{
 public void mouseMoved(MouseEvent event){
 //do something
 }
 public void mouseDragged(MouseEvent event){
 //do something
 }
 }
}

Now Lets say a mouse drag event occurs. How does the MouseMotionHandler knows which method to call. of the two methods implemented. Or how is the method to be called resolved in run-time when an event occurs.

If the MouseEvent event that gets passed to these method is MouseDrag Event, how is that only mouseDragged is called.

and how does it know that it is a MouseDrag event and not MouseMove event?

mKorbel
110k20 gold badges138 silver badges331 bronze badges
asked Jan 14, 2014 at 5:26
8
  • Why do you use Swing? It's age old and hasn't been updated for ten years. JavaFX is the new kid on the block. Commented Jan 14, 2014 at 5:36
  • 5
    @MartinAndersson Because it's has a well supported community that knows how it works and many millions of people hours of active, production experience... Commented Jan 14, 2014 at 5:46
  • 1
    @MartinAndersson: which one is open source? Which one is proprietary? Commented Jan 14, 2014 at 7:29
  • 1
    @MartinAndersson "Why do you use Swing? It's age old and hasn't been updated for ten years." That is complete rubbish. Perhaps you should stick to what you know. "JavaFX is the new kid on the block." Yes.. note that both Sun & Oracle have a nasty habit of hawking some new tech. as 'the next big thing' only to quietly abandon it later. I'll trust them to stand by Java-FX when they have: 1) Incorporated it into the Java Tutorial 2) List the API in the JSE Java Docs. - Until then, it's a crap shoot. Commented Jan 14, 2014 at 8:28
  • 1
    @MartinAndersson its possible that JavaFX might succeed, nut it's the user base that makes these decisions, not the corporations. MS has had to abandon a number of its "next big" development environments in recent history because of a lack of interest. When Java moving into the open source community Sun/Oracle can make all the statements the like... Commented Jan 14, 2014 at 10:04

2 Answers 2

3

The long and short of it...

The AWT core starts a native "event loop". This loop basically takes in events from the OS and processes them. If the event is of interest to the current application context, the event is processed and added to the Event Queue.

The Event Queue is processed by the Event Dispatching Thread, which dispatches the event to the appropriate listener, based on who the event was for.

This is a significant simplification of the process.

None-the-less, when an event comes into the native "event loop", it's properties are inspected and an appropriate AWT Event is generated. How this is determined comes down a lot to how the OS passes it's event information, but basically, if the OS detects a drag, the MouseEvent has it's ID property set to MouseEvent.MOSUE_DRAGGED, this allows the Component to sift through the events and determine the best listener it should notify, which comes out to be MouseMotionListener.mouseDragged(MouseEvent)

For example, this is the processMouseMotionEvent method take from Component

protected void processMouseMotionEvent(MouseEvent e) {
 MouseMotionListener listener = mouseMotionListener;
 if (listener != null) {
 int id = e.getID();
 switch(id) {
 case MouseEvent.MOUSE_MOVED:
 listener.mouseMoved(e);
 break;
 case MouseEvent.MOUSE_DRAGGED:
 listener.mouseDragged(e);
 break;
 }
 }
}
answered Jan 14, 2014 at 7:44
Sign up to request clarification or add additional context in comments.

Comments

3

mouseDragged and mouseMoved events are different in terms of whether mouse button is pressed or not. Here is the description of both the methods:

mouseDragged(MouseEvent)
Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.

mouseMoved(MouseEvent)
Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.

Here is an excellent tutorial about handling mouse events:

http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

answered Jan 14, 2014 at 5:28

4 Comments

my question is not what these events are? but how the appropriate method is resolved for call is my doubt..
@user1988876 events are raised by the native code and provided to JVM, which inturn forwards it to the interested java class. Java class such as MouseListener use that event to call the appropriate event as per the properties of event.
MouseMotionListener has two methods here. how is the right method called? I am a little confused here. I mean how the right method associated with that particular event object is called and not the other..
@user1988876 If you have a JPanel that you created and registered as a MouseMotionListener to, then it will call the appropriate method based on the criteria. The general gist of it is that it checks to see what the mouse is doing, decides on what method it should call, then loops through it's list of MouseMotionListeners, calling the appropriate method on each. If you happened to "sign up" to be on that list, then you'll be one of the objects to have your method called based on the event.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.