Java Program to Handle MouseEvent

»

This is a Java program to handle MouseEvent.

Problem Description

We have to write a program in Java such that it demonstrates the event actions associated with a mouse. The program should demonstrate various mouse events such as mouse clicked event, mouse pressed event, mouse released event, mouse entered event and mouse exited event.

Expected Input and Output

For handling MouseEvent, we can have the following 5 different sets of input and output.

advertisement

1. To test mouseClicked: When the mouse is clicked at a point on frame.

For example:

If the left button of the mouse is clicked at any point (x,y) on the frame
then the expected output is "Left Button Clicked at point x y"

2. To test mousePressed: When the mouse is pressed at a point on frame.

For example:

If the left button of the mouse is kept pressed at any point (x,y) on the frame
then the expected output is "Left Button Pressed at point x y"

3. To test mouseReleased: When the mouse is released at a point on frame after being kept pressed.

advertisement

For example:

If the left button of the mouse is released at any point (x,y) on the frame
then the expected output is "Left Button Released at point x y"

4. To test mouseEntered: When the mouse enters the frame.

For example:

If the mouse enters into the frame from any point (x,y) on the frame
then the expected output is "Mouse Entered the frame from point x y"

5. To test mouseExited: When the mouse leaves the frame.

advertisement

For example:

If the mouse leaves the frame from any point (x,y) on the frame
then the expected output is "Mouse Exited the frame from point x y"
Problem Solution

1. The program uses the interfaces MouseListener and ActionListener of the java.awt package.
a) The MouseListener interface has five member methods :
i) public void mouseEntered(MouseEvent): This method gives the co-ordinates of the point from where the mouse has entered the frame.
ii) public void mouseExited(MouseEvent): This method gives the co-ordinates of the point from where the mouse has left the frame.
iii) public void mouseReleased(MouseEvent): This method gives the co-ordinates of the point till where the mouse cursor was dragged before release and also the button clicked on the mouse.
iv) public void mousePressed(MouseEvent): This method gives the co-ordinates of the point from where the mouse was pressed and also the button.
v) public void mouseClicked(MouseEvent): This method gives the co-ordinates of the point where the mouse cursor was clicked and also the button clicked.

b) The ActionListener interface has member method :
public void actionPerformed(ActionEvent): This method works on the click of the exit button and the functions closes the frame.

2. @Override is a keyword used to override any method on the parent class. When the sub class has any method defined in parent/super class with same name and parameters, the Override keyword is used. This overrides the function of super class and executed the function defined in the sub class.

3. Create a class that implements the two required interfaces – MouseListener and ActionListener.
4. Create a frame, text field and an exit button with required dimensions. After positioning the co-ordinates of the text field and button, add them to the frame.
5. Associate MouseListener with the frame and ActionListener with the exit button. Display the frame.
6. The functions getX() and getY() gives the x & y co-ordinates of the cursor respectively. Use them to display the necessary message in the text box.

Program/Source Code

Here is source code of the Java Program to handle the mouse events. The program is successfully compiled and tested using BlueJ on Windows 10 and javac compiler on Fedora 30.

  1. /* Java Program to demonstrate the event actions associated with a mouse */
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. public class Mouse_Event implements MouseListener,ActionListener
  6. {
  7.     static JFrame frame;
  8.     static JTextField text;
  9.     //Driver function
  10.     public static void main(String[] args)
  11.     {
  12.         //Create a Frame
  13.         frame=new JFrame("Mouse Event");
  14.         frame.setBackground(Color.white);
  15.         frame.setSize(500,500);
  16.         frame.setLayout(null);
  17.         //Create a TextField
  18.         text=new JTextField();
  19.         text.setBounds(0,0,500,50);
  20.         frame.add(text);
  21.         //Create a exit button to close the frame
  22.         JButton exit=new JButton("Exit");
  23.         exit.setBounds(220,235,60,30);
  24.         frame.add(exit);
  25.         //Create an object of the class Mouse_Event
  26.         Mouse_Event obj=new Mouse_Event();
  27.         //Associate MouseListener with the frame
  28.         frame.addMouseListener(obj);
  29.         //Associate ActionListener with button exit
  30.         exit.addActionListener(obj);
  31.         //Display frame
  32.         frame.setVisible(true);
  33.     }
  34.     //function to dispose the frame on click of exit button
  35.     @Override
  36.     public void actionPerformed(ActionEvent e)
  37.     {
  38.         frame.dispose();
  39.     }
  40.     //function to get co-ordinates from where cursor entered the frame
  41.     @Override
  42.     public void mouseEntered(MouseEvent e)
  43.     {
  44.         text.setText("");
  45.         text.setText("Mouse Entered the frame from point ");
  46.         text.setText(text.getText()+e.getX()+" "+e.getY());
  47.     }
  48.     //function to get co-ordinates from where cursor exited the frame
  49.     @Override
  50.     public void mouseExited(MouseEvent e)
  51.     {
  52.         text.setText("");
  53.         text.setText("Mouse Exited the frame from point ");    
  54.         text.setText(text.getText()+e.getX()+" "+e.getY());
  55.     }
  56.     //function to get co-ordinates where mouse was released
  57.     @Override
  58.     public void mouseReleased(MouseEvent e)
  59.     {
  60.         text.setText("");
  61.         String button="Right";
  62.         if(e.getButton()==MouseEvent.BUTTON1)
  63.             button="Left";
  64.         text.setText(button+" Button Released at point ");
  65.         text.setText(text.getText()+e.getX()+" "+e.getY());
  66.     }
  67.     //function to get co-ordinates where and which button of mouse was pressed
  68.     @Override
  69.     public void mousePressed(MouseEvent e)
  70.     {
  71.         text.setText("");
  72.         String button="Right";
  73.         if(e.getButton()==MouseEvent.BUTTON1)
  74.             button="Left";
  75.         text.setText(button+" Button Pressed at point ");
  76.         text.setText(text.getText()+e.getX()+" "+e.getY());     
  77.     }
  78.     //function to get co-ordinates where and which button of mouse was clicked
  79.     @Override
  80.     public void mouseClicked(MouseEvent e)
  81.     {
  82.         text.setText("");
  83.         String button="Right";
  84.         if(e.getButton()==MouseEvent.BUTTON1)
  85.             button="Left";
  86.         text.setText(button+" Button Clicked at point ");
  87.         text.setText(text.getText()+e.getX()+" "+e.getY());    
  88.     }
  89. }
Program Explanation

1. The program demonstrates the functions of the MouseListener interface.
a) When the cursor enters the frame, the mouseEntered function is called.
b) When the cursor exits the frame, the mouseExited function is called.
c) When the cursor is released after being pressed, the mouseReleased function is called.
d) When the cursor is kept pressed at the frame, the mousePressed function is called.
e) When the cursor is clicked at a point on the frame, the mouseClicked function is called.

advertisement

2. The @Override keyword overrides the functions of the parent class and executes the functions of sub class.

Runtime Test Cases

Here’s the run time test cases for mouse events.

Test case 1 – Here’s the runtime output of the button clicked operation.
java-program-mouse-event-left-button-clicked

Test case 2 – Here’s the runtime output of the button pressed operation.
java-program-mouse-event-left-button-pressed

Test case 3 – Here’s the runtime output of the button released operation.
java-program-mouse-event-left-button-released

Test case 4 – Here’s the runtime output of the mouse entered operation.
java-program-mouse-event-mouse-entered

Test case 5 – Here’s the runtime output of the mouse exited operation.
java-program-mouse-event-mouse-exited

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn