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
  • 109,525
  • 20
  • 134
  • 319
brain storm
  • 30,124
  • 69
  • 225
  • 393
  • 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. – Martin Andersson Jan 14 '14 at 05: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... – MadProgrammer Jan 14 '14 at 05:46
  • @MadProgrammer: any thoughts here.. – brain storm Jan 14 '14 at 05:59
  • 1
    @MartinAndersson: which one is open source? Which one is proprietary? – Hovercraft Full Of Eels Jan 14 '14 at 07:29
  • Plenty, none of which might be helpful ;) – MadProgrammer Jan 14 '14 at 07:45
  • 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](http://docs.oracle.com/javase/tutorial/) 2) List the API in the [JSE Java Docs.](http://docs.oracle.com/javase/7/docs/api/) - Until then, it's a crap shoot. – Andrew Thompson Jan 14 '14 at 08:28
  • I hope they never incorporate JavaFX into the Java tutorial. Okay, maybe a trail or two. I consider [this](http://docs.oracle.com/javafx/) a very good "tutorial" on the topic. The JavaFX guides is just way too much to include in an already huge "tutorial". Likewise, I hope they keep the API separate. However, I've read somewhere that they plan on merging the API:s too? Anyways, you know you have to make the switch eventually. From [Oracle](http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html#6): "**Is JavaFX replacing Swing as the new client UI library for Java SE? Yes.**" – Martin Andersson Jan 14 '14 at 08:50
  • 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... – MadProgrammer Jan 14 '14 at 10:04

2 Answers2

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

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • my question is not what these events are? but how the appropriate method is resolved for call is my doubt.. – brain storm Jan 14 '14 at 05:43
  • @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. – Juned Ahsan Jan 14 '14 at 05:45
  • `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.. – brain storm Jan 14 '14 at 05:49
  • @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. – Andrew Gies Jan 14 '14 at 06:33
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;
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366