Advertisement



< Prev
Next >



MouseEvent and MouseMotionListener





In order to handle mouse motion events in an appliction i.e. events which are generated when a mouse is moved, we need to work with MouseEvent class. An event of type MouseEvent class is generated in mouse motion situations like -
In order to create a class that handles mouse motion events, we need to understand some important methods of MouseEvent class in the table below.



Some methods of MouseEvent class


Method Description
public Point getLocationOnScreen() Returns the absolute x, y position of the MouseEvent.
public int getX() Returns the x-coordinate of the MouseEvent.
public int getY() Returns the y-coordinate of the MouseEvent.






A class to listen and respond to event type MouseEvent, must perform two steps:


Method Description
public void addMouseMotionListener(MouseMotionListener object) where object is an object of the class that has implemented MouseMotionListener interface. Doing this, registers the class to listen and respond to mouse move and drag MouseEvent.



Advertisement




Handling an MouseEvent by implementing MouseListener interface


In the upcoming example, we are going to handle mouse motion events such as -
import java.awt.*;
import java.awt.event.*;
import java.awt.Point;


public class MouseEx3 extends WindowAdapter implements MouseMotionListener
{
Label label1, label2;
Frame frame;
String str;
Point p;


MouseEx3()
{

frame = new Frame("Window");

label1= new Label("Tracking mouse cursor in the Frame window", Label.CENTER);
label2= new Label();


frame.setLayout(new FlowLayout());
frame.add(label1);
frame.add(label2);



//Registering class MouseEx1 to catch and respond to mouse motion motion events 
frame.addMouseMotionListener(this);



//Registering class MouseEx3 to catch and respond to window event i.e. window closing event
frame.addWindowListener(this);


frame.setSize(280,200);
frame.setVisible(true);
}


//Method of WindowAdapter class to catch and respond to window closing event
public void windowClosing(WindowEvent we)
{
    System.exit(0);
}


//Method of MouseMotionListener interface
public void mouseDragged(MouseEvent me)
{
String s = me.getX() + "," + me.getY();
label2.setText("Mouse dragged "+ s);
frame.setVisible(true);
}


//Method of MouseMotionListener interface
public void mouseMoved(MouseEvent me)
{
String s = me.getX() + "," + me.getY();
label2.setText("Mouse moved "+ s);
frame.setVisible(true);
}



public static void main(String... ar)
{
new MouseEx3();
}

}

When you run the code, you are presented a window shown in the window below -:

Figure 1



When you move your mouse cursor in the Frame's window area, it generates an event i.e. MouseEvent. Method mouseMoved() is executed to handle mouse movement event and as you move your mouse cursor, you are notified about the current x and y coordinates of your mouse cursor in the window :

Figure 2



When you drag your mouse cursor within the Frame's window area, it generates a MouseEvent, the method mouseDragged() is executed to handle mouse drag event and you are notified about current x and y coordinates of your mouse cursor in the window, where the mouse is dragged :

Figure 3




Please share this article -





< Prev
Next >
< MouseListener
ItemListener >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement