This is a Java program to handle MouseEvent.
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.
For handling MouseEvent, we can have the following 5 different sets of input and output.
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.
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.
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"
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.
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.
/* Java Program to demonstrate the event actions associated with a mouse */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mouse_Event implements MouseListener,ActionListener
{
static JFrame frame;
static JTextField text;
//Driver function
public static void main(String[] args)
{
//Create a Frame
frame=new JFrame("Mouse Event");
frame.setBackground(Color.white);
frame.setSize(500,500);
frame.setLayout(null);
//Create a TextField
text=new JTextField();
text.setBounds(0,0,500,50);
frame.add(text);
//Create a exit button to close the frame
JButton exit=new JButton("Exit");
exit.setBounds(220,235,60,30);
frame.add(exit);
//Create an object of the class Mouse_Event
Mouse_Event obj=new Mouse_Event();
//Associate MouseListener with the frame
frame.addMouseListener(obj);
//Associate ActionListener with button exit
exit.addActionListener(obj);
//Display frame
frame.setVisible(true);
}
//function to dispose the frame on click of exit button
@Override
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
//function to get co-ordinates from where cursor entered the frame
@Override
public void mouseEntered(MouseEvent e)
{
text.setText("");
text.setText("Mouse Entered the frame from point ");
text.setText(text.getText()+e.getX()+" "+e.getY());
}
//function to get co-ordinates from where cursor exited the frame
@Override
public void mouseExited(MouseEvent e)
{
text.setText("");
text.setText("Mouse Exited the frame from point ");
text.setText(text.getText()+e.getX()+" "+e.getY());
}
//function to get co-ordinates where mouse was released
@Override
public void mouseReleased(MouseEvent e)
{
text.setText("");
String button="Right";
if(e.getButton()==MouseEvent.BUTTON1)
button="Left";
text.setText(button+" Button Released at point ");
text.setText(text.getText()+e.getX()+" "+e.getY());
}
//function to get co-ordinates where and which button of mouse was pressed
@Override
public void mousePressed(MouseEvent e)
{
text.setText("");
String button="Right";
if(e.getButton()==MouseEvent.BUTTON1)
button="Left";
text.setText(button+" Button Pressed at point ");
text.setText(text.getText()+e.getX()+" "+e.getY());
}
//function to get co-ordinates where and which button of mouse was clicked
@Override
public void mouseClicked(MouseEvent e)
{
text.setText("");
String button="Right";
if(e.getButton()==MouseEvent.BUTTON1)
button="Left";
text.setText(button+" Button Clicked at point ");
text.setText(text.getText()+e.getX()+" "+e.getY());
}
}
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.
2. The @Override keyword overrides the functions of the parent class and executes the functions of sub class.
Here’s the run time test cases for mouse events.
Test case 1 – Here’s the runtime output of the button clicked operation.
Test case 2 – Here’s the runtime output of the button pressed operation.
Test case 3 – Here’s the runtime output of the button released operation.
Test case 4 – Here’s the runtime output of the mouse entered operation.
Test case 5 – Here’s the runtime output of the mouse exited operation.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Programming Books
- Check Java Books
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Java Internship