javacodex.com
Java Examples
Java Examples
memu home questions

MouseListener

The mouse pressed, released, and clicked events are triggered when the mouse is pressed, released, and clicked on a component. When a mouse enters the component’s bound, a mouse entered event is triggered. A mouse exited event is triggered when a mouse leaves the component’s bounds.

Source: (MouseListenerExample.java)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class MouseListenerExample {
  public static void main(String [] args) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("MouseListener Example");
    frame.setSize(300,150);
 
    final JLabel label = new JLabel();
    frame.add(label);
 
    frame.addMouseListener(new MouseListener() {
        public void mousePressed(MouseEvent me) { 
          label.setText("Mouse Pressed");
        }
        public void mouseReleased(MouseEvent me) { 
          label.setText("Mouse Released");
        }
        public void mouseEntered(MouseEvent me) { 
          label.setText("Mouse Entered");
        }
        public void mouseExited(MouseEvent me) { 
          label.setText("Mouse Exited");
        }
        public void mouseClicked(MouseEvent me) { 
          label.setText("Mouse Clicked");
        }
    });
 
    frame.setVisible(true);			
  }
}
 

Output:

$ java MouseListenerExample
Output



Contact: javacodex@yahoo.com