1. Technology

Capturing Mouse Events Example Program


Java Code

This example Java code demonstrates implementing the MouseListener, MouseMotionListener and MouseWheelListener event listeners. The application is made up of a JFrame containing two JLabels, and a JTextArea. The two JLabels implement the MouseListener and MouseMotionListener interfaces and a JScrollPane containing the JTextArea implements the MouseWheelListener

The step-by-step guide that goes with this code is called How to Handle Mouse Events.

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseMotionListener;

public class MouseLabel {

    JLabel mouseLabel;
    JLabel mouseMoveLabel;
    JTextArea mouseEvents;
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new MouseLabel();         
    public MouseLabel()
        JFrame guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("Mouse Event Example");
        //This will center the JFrame in the middle of the screen
        //creating a border to highlight the label areas
        Border outline = BorderFactory.createLineBorder(Color.black);
        //mouseLabel will trigger the Mouse click evetns
        mouseLabel = new JLabel("Interactive Label", JLabel.CENTER);
        //Attach the MouseListener to mouseLabel 
        //as an anonymous inner class.
        //Each method changes the test of mouseLabel and
        //logs the mouse event in the JTextArea
        mouseLabel.addMouseListener(new MouseListener()
            public void mouseClicked(MouseEvent e)
                mouseLabel.setText("I've been clicked!");
                mouseEvents.append("MouseClicked Event");
                mouseEvents.append(e.getClickCount() + " click(s)\n");
                mouseEvents.append("Xpos: " + e.getX() + " Ypos: " + e.getY() + "\n");
            public void mousePressed(MouseEvent e)
                mouseLabel.setText("You're holding the mouse button aren't you?");
                mouseEvents.append("MousePressed Event\n");
            public void mouseExited(MouseEvent e)
                mouseLabel.setText("The mouse has run away!");
                mouseEvents.append("MouseExited Event\n");
            public void mouseEntered(MouseEvent e)
                mouseLabel.setText("I can feel the presence of the Mouse");
                mouseEvents.append("MouseEntered Event\n");
            public void mouseReleased(MouseEvent e)
                mouseLabel.setText("You've let go of the mouse button");
                mouseEvents.append("MouseReleased Event\n");
        //mouseMoveLabel triggers the mouse motion events
        mouseMoveLabel = new JLabel("Drag the Mouse!");
        //Attach the MouseMotionListener to mouseMoveLabel 
        //as an anonymous inner class.
        //Each method logs the events triggered in the JTextArea
        mouseMoveLabel.addMouseMotionListener(new MouseMotionListener(){
            public void mouseDragged(MouseEvent e)
                mouseEvents.append("MouseDragged Event\n");

            public void mouseMoved(MouseEvent e)
                mouseEvents.append("MouseMoved Event\n");
        mouseEvents = new JTextArea("The Mouse events can be seen here:\n");
        //Place the JTextArea into a JScrollPane to be able to scroll
        //through all the events logged. It's also perfect for listening
        //for MouseWheelListener events
        JScrollPane textScroll = new JScrollPane(mouseEvents);
        //Attach the MouseWheelListener to textScroll 
        //as an anonymous inner class.
        //As some many events are fired by the mouse moving the method 
        //just changes the text of mouseLabel rather than logging them
        //all in the JTextArea.
        textScroll.addMouseWheelListener(new MouseWheelListener(){
            public void mouseWheelMoved(MouseWheelEvent e)
                mouseLabel.setText("You've moved the mouse wheel");
        guiFrame.add(mouseMoveLabel, BorderLayout.WEST);
        guiFrame.add(mouseLabel, BorderLayout.CENTER);
        guiFrame.add(textScroll, BorderLayout.EAST);


  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Handling Events
  6. Capturing Mouse Events Example Program

©2014 About.com. All rights reserved.