
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we detect an event when the mouse moves over any component in Java?
In this article, we will learn to detect an event when the mouse moves over any component in Java. While building applications with Swing, detecting when the mouse enters or exits a component enables you to create responsive UIs with visual feedback.
MouseListener
We can implement a MouseListener interface when the mouse is stable while handling the mouse event. A MouseEvent is fired when we can press, release, or click (press followed by release) a mouse button (left or right button) at the source object or position the mouse pointer at (enter) and away (exit) from the source object.
mouseEntered() Method
The mouseEntered() method is called when the mouse enters a component.
Method Declaration:
void mouseEntered(MouseEvent e)
mouseExited() Method
The mouseExited() method is called when the mouse exits a component.
Method Declaration:
void mouseExited(MouseEvent e)
Detect an Event on the Mouse Movement
We can detect a mouse event when the mouse moves over any component, such as a label, by using the mouseEntered() method and can be exited by using the mouseExited() method of the MouseAdapter class or MouseListener interface.
The following are the steps for detecting an event when the mouse moves over any component in Java:
Label Creation
Creates a JLabel with text& "Move the mouse over this JLabel", then makes the label's background visible by setting the setOpaque(true) method, and adds the label to the JFrame.
label = new JLabel("Move the mouse over this JLabel"); label.setOpaque(true); add(label);
Mouse Enter Event
When the mouse enters the label area(mouseEntered) using the MouseListener interface, it swaps the label's background and text colors with the foreground color to create an effect that the mouse is moved(Hover effect).
public void mouseEntered(MouseEvent evt) { Color c = label.getBackground(); label.setBackground(label.getForeground()); label.setForeground(c); }
Mouse Exit Event
When the mouse leaves the label area(mouseExited) using the MouseListener interface, it swaps the colors back to the original color.
public void mouseExited(MouseEvent evt) { Color c = label.getBackground(); label.setBackground(label.getForeground()); label.setForeground(c);
}
Example
Below is an example of detecting an event when the mouse moves over any component in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseOverTest extends JFrame { private JLabel label; public MouseOverTest() { setTitle("MouseOver Test"); setLayout(new FlowLayout()); label = new JLabel("Move the mouse moves over this JLabel"); label.setOpaque(true); add(label); label.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent evt) { Color c = label.getBackground(); // When the mouse moves over a label, the background color changed. label.setBackground(label.getForeground()); label.setForeground(c); } public void mouseExited(MouseEvent evt) { Color c = label.getBackground(); label.setBackground(label.getForeground()); label.setForeground(c); } }); setSize(400, 275); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MouseOverTest(); } }