How to implement MouseListener in Java

The MouseListener in Java takes actions whenever the user hovers the mouse to the component, clicked, pressed, released, entered, or exited the component.

You need to implement and override methods like mouseClicked, mousePressed, mouseReleased, mouseEntered, and mouseExited to execute codes whenever a mouse action was performed.

Implementing a MouseListener in Java

package com.javapointers.javase;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class MouseListenerTest implements MouseListener {

    JPanel leftPanel, rightPanel;
    JTextArea textArea;
    JFrame frame;

    public MouseListenerTest() {
        frame = new JFrame("MouseListener Test");
        textArea = new JTextArea();
        textArea.setLineWrap(true);

        leftPanel = new JPanel();
        rightPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(200, 100));
        rightPanel.setPreferredSize(new Dimension(200, 100));
        leftPanel.setBackground(Color.GREEN);
        rightPanel.setBackground(Color.BLUE);

        leftPanel.addMouseListener(this);
        rightPanel.addMouseListener(this);

        frame.setLayout(new BorderLayout());
        frame.add(leftPanel, BorderLayout.WEST);
        frame.add(rightPanel, BorderLayout.EAST);
        frame.add(textArea, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have clicked the left panel");
        } else {
            textArea.setText("You have clicked the right panel");
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have pressed the left panel");
        } else {
            textArea.setText("You have pressed the right panel");
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have released mouse on left panel");
        } else {
            textArea.setText("You have released mouse on right panel");
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have entered mouse on left panel");
        } else {
            textArea.setText("You have entered mouse on right panel");
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have exited mouse on left panel");
        } else {
            textArea.setText("You have exited mouse on right panel");
        }
    }

    public static void main(String args[]) {
        MouseListenerTest test = new MouseListenerTest();
    }
}

Running our program will result in a frame that will have 2 panels with a text area that will display what panels have caused the action:

Explanation:

  • implements MouseListener

before we can use the MouseListener class, we need to implement it first.

  • leftPanel.addMouseListener(this);

means to add leftPanel to the components being monitored/listened to any mouse events.

  • public void mouseClicked(MouseEvent e){}

handles events when the user clicked the component.

  • public void mousePressed(MouseEvent e) {}

it handles events when the user pressed the component

  • public void mouseReleased(MouseEvent e) {}

this method will be called when the user released the components from pressing.

  • public void mouseEntered(MouseEvent e) {}

called when the user entered or hover the mouse to the component.

  • public void mouseExited(MouseEvent e) {}

it will be called when the user exited or remove the mouse pointer from being hovered.

The next tutorial will be about Key Listener that handles key events such that whenever the user presses a key in the keyboard.

Share this tutorial!