Advertisement



< Prev
Next >



MouseEvent and MouseListener





An event of type MouseEvent is generated in a situations when -




Some methods of MouseEvent class


Method Description
public int getX() Returns the x-coordinate of the MouseEvent.
public int getY() Returns the y-coordinate of the MouseEvent.





A class to listen & respond to a MouseEvent, must perform the next two steps -


Method Description
public void addMouseListener(ItemListener object) where object is an object of the class that wants to listen and respond to MouseEvent and has implemented MouseListener interface. This registers the class to listen & respond to MouseEvent.



Advertisement




Handling an MouseEvent by implementing MouseListener interface


In the upcoming code, we are going to create a class by implementing MouseListener interface, to listen to MouseEvent-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MouseEx1 implements MouseListener
{
Label label1, label2;
Frame frame;
String str;

MouseEx1()
{
frame = new Frame("Window");
label1= new Label("Handling mouse events 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 events 
frame.addMouseListener(this); 

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


public void mouseClicked(MouseEvent we)
{
str+=" and Mouse button was clicked";
label2.setText(str);
frame.setVisible(true);
}

public void mouseEntered(MouseEvent we)
{
label2.setText("Mouse has entered the window area");
frame.setVisible(true);
}

public void mouseExited(MouseEvent we)
{
label2.setText("Mouse has exited the window area");
frame.setVisible(true);
}


public void mousePressed(MouseEvent we)
{
label2.setText("Mouse button is being pressed");
frame.setVisible(true);
}

public void mouseReleased(MouseEvent we)
{
str="Mouse button is released";
label2.setText(str);
frame.setVisible(true);
}


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

}

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 a MouseEvent. The following method is executed and and you are notified about it in the window :

Figure 2



When you move your mouse cursor out of the Frame's window area, it generates a MouseEvent. The following method is executed and and you are notified about it in the window :

Figure 3



When you move your mouse cursor back in the Frame's window area and press and hold any of mouse's button, it generates a MouseEvent. The following method is executed and and you are notified about it in the window :

Figure 4



When you release the pressed mouse button, it generates a MouseEvent. The following method are executed in sequence and and you are notified about it in the window :

Figure 5




Please share this article -





< Prev
Next >
< KeyListener
MouseMotionListener >



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

PYTHON


Advertisement