1
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class AppWindow extends Frame {

    String keyMessage = "";
    String MouseMsg = "";
    int mouseX = 10;
    int mouseY = 40;
    int locX = 0;
    int locY = 0;

    public AppWindow() {
        addMouseListener(new MyMouseAdaptor(this));
    }

    public void paint(Graphics g) {
        g.drawString(keyMessage, mouseX, mouseY);
        g.drawString(MouseMsg, locX, locY);
    }

    public static void main(String[] args) {
        AppWindow appWindow = new AppWindow();
        appWindow.setSize(400, 400);
        appWindow.setVisible(true);
    }
}

class MyMouseAdaptor extends MouseAdapter implements MouseListener {
    AppWindow appWindow;

    public MyMouseAdaptor(AppWindow appWindow) {
        this.appWindow = appWindow;
    }

    public void mousePressed(MouseEvent e) {
        this.appWindow.MouseMsg = "Mouse Pressed at : " + e.getX() + ", "
                + e.getY();
        this.appWindow.locX = e.getX();
        this.appWindow.locY = e.getY();
        this.appWindow.repaint();
    }
}    

Dear All

I have a weird question. I know everything in the above code yet I am missing something. How Java knows when the mousePressed Event occurred? I need to find the answer for my own logic. Where is the code written that says

when the user press the mouse -- > trigger the method "public void mousePressed(MouseEvent e)" and do what is inside it

Thanks

Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50

2 Answers2

1

This is the code that registers to look out for mouse events:

public AppWindow() {
    addMouseListener(new MyMouseAdaptor(this)); 
}

This is your class that extends MouseAdaptor and listens for events:

class MyMouseAdaptor extends MouseAdapter implements MouseListener {
    AppWindow appWindow;

    public MyMouseAdaptor(AppWindow appWindow) {
        this.appWindow = appWindow;
    }

    public void mousePressed(MouseEvent e) {
        this.appWindow.MouseMsg = "Mouse Pressed at : " + e.getX() + ", "
            + e.getY();
        this.appWindow.locX = e.getX();
        this.appWindow.locY = e.getY();
        this.appWindow.repaint();
    }
}    

MouseAdaptor:

An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects. Mouse events let you track when a mouse is pressed, released, clicked, moved, dragged, when it enters a component, when it exits and when a mouse wheel is moved.

Extend this class to create a MouseEvent (including drag and motion events) or/and MouseWheelEvent listener and override the methods for the events of interest. (If you implement the MouseListener, MouseMotionListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)

Create a listener object using the extended class and then register it with a component using the component's addMouseListener addMouseMotionListener, addMouseWheelListener methods. The relevant method in the listener object is invoked and the MouseEvent or MouseWheelEvent is passed to it in following cases:

  • when a mouse button is pressed, released, or clicked (pressed and released)
  • when the mouse cursor enters or exits the component
  • when the mouse wheel rotated, or mouse moved or dragged

Link

MouseListener:

The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track mouse moves and mouse drags, use the MouseMotionListener.) The class that is interested in processing a mouse event either implements this interface (and all the methods it contains) or extends the abstract MouseAdapter class (overriding only the methods of interest).

The listener object created from that class is then registered with a component using the component's addMouseListener method. A mouse event is generated when the mouse is pressed, released clicked (pressed and released). A mouse event is also generated when the mouse cursor enters or leaves a component. When a mouse event occurs, the relevant method in the listener object is invoked, and the MouseEvent is passed to it.

Link

Now after you have read this, I think you will be able to make some changes to your program because when you implement MouseListener interface you have to define all of the methods in it..

Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
  • Dear . Thanks for you answer .. I know all the you have mentioned .. My question is .. How java knows when the method is pressed as there is a code that build-in in java that says (when the user press the mouse -- > trigger the method "public void mousePressed(MouseEvent e)" and do what is inside it ) .. Please think about it .. – Waseem Swaileh Sep 28 '14 at 05:07
  • As I have written in the answer, The MouseAdaptor is an abstract class which is used for creating listener objects which listens for any mouse events and as soon as that event happens the corresponding method is called and mouse event is passed to it. Same goes for interface MouseListener. – Syed Farjad Zia Zaidi Sep 28 '14 at 05:15
  • I think EventListener is where the code is written which lets Java listen to events and invoke corresponding methods, since all event listener extend EventListener. – Syed Farjad Zia Zaidi Sep 28 '14 at 05:19
  • 1
    Thanks Dear .. You lead me to the answer .. actually it's implemented in the Component class .. Thanks Alot – Waseem Swaileh Sep 28 '14 at 05:29
0

There are actually two event classes associated with the mouse: MouseEvent and MouseMotionEvent. There are also two listener interfaces, MouseListener and MouseMotionListener. The MouseListener interface declares the methods

      public void mousePressed(MouseEvent evt);
      public void mouseReleased(MouseEvent evt);
      public void mouseClicked(MouseEvent evt);
      public void mouseEntered(MouseEvent evt);
      public void mouseExited(MouseEvent evt);

and the MouseMotionListener declares

      public void mouseMoved(MouseEvent evt);
      public void mouseDragged(MouseEvent evt);

Any component can generate mouse events. An object that wants to respond to these events must implement one or both of the listener interfaces. It must also register itself with the component by calling the component's addMouseListener() and/or addMouseMotionListener() methods. Note that an object that implements MouseListener must provide definitions for all five of the methods in that interface, even if a definition consists just of an empty set of braces. Similarly, an object that implements MouseMotionListener must define both the mouseMoved() and the mouseDragged() methods.

A component calls mousePressed() whenever one of the buttons on the mouse is pressed while the mouse cursor is over that component. It will then call the mouseReleased() method when the button is released -- even if the cursor has moved outside of the component by that time. The mouseClicked() method is called if the button is pressed and released at the same point; it is called in addition to mousePressed() and mouseReleased(). If you simply want to respond to mouse clicks, you should probably do so in the mousePressed() routine, and leave the definitions of mouseReleased() and mouseClicked() empty.

Source

Pert8S
  • 582
  • 3
  • 6
  • 21
  • Dear . Thanks for you answer .. I know all the you have mentioned .. My question is .. How java knows when the method is pressed as there is a code that build-in in java that says (when the user press the mouse -- > trigger the method "public void mousePressed(MouseEvent e)" and do what is inside it ) .. Please think about it .. – Waseem Swaileh Sep 28 '14 at 05:08
  • Bro if you are digging inside JVM and JRE then help yourself.Because there are hundreds of different vendors who develop their own internal logic. : ) – Pert8S Sep 28 '14 at 05:26