event

MouseListener example

In this example we are going to see how you can use the MouseListener interface in Java, in order to monitor the mouse activity on your application. This is very useful when you wan to add extra functionality to you application and make it more agile, when you make it possible for the user to interact with it using the mouse.

It’s very easy to use a MouseListener. All you have to do is:

  • Create a new MouseListener.
  • Override the methods that correspond to the mouse events you want to monitor, e.g mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased etc.
  • Use the addMouseListener method of your component to monitor the mouse activity that occurs on it.
  • Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class MouseListenerExample extends JFrame {

    int startX, startY, endX, endY;

    Color red_col = Color.red;

    public MouseListenerExample() {

  super();

  final JPopupMenu popMenu = new JPopupMenu();

  popMenu.add(new JMenuItem("Cut"));

  popMenu.add(new JMenuItem("Copy"));

  popMenu.add(new JMenuItem("Paste"));

  popMenu.addSeparator();

  popMenu.add(new JMenuItem("Select All"));

  popMenu.setInvoker(this);

  MouseListener popup = new MouseListener() {

@Override

public void mouseClicked(MouseEvent event) {

}

@Override

public void mouseEntered(MouseEvent event) {

}

@Override

public void mouseExited(MouseEvent event) {

}

@Override

public void mousePressed(MouseEvent event) {

    if (event.isPopupTrigger()) {

  popMenu(event);

    }

}

@Override

public void mouseReleased(MouseEvent event) {

    if (event.isPopupTrigger()) {

  popMenu(event);

    }

}

private void popMenu(MouseEvent event) {

    popMenu.show(event.getComponent(), event.getX(), event.getY());

}

  };

  addMouseListener(popup);

  MouseListener drawing1 = new MouseListener() {

@Override

public void mouseClicked(MouseEvent event) {

}

@Override

public void mouseEntered(MouseEvent event) {

}

@Override

public void mouseExited(MouseEvent event) {

}

@Override

public void mousePressed(MouseEvent event) {

    red_col = Color.RED;

    startX = endX = event.getX();

    startY = endY = event.getY();

    repaint();

}

@Override

public void mouseReleased(MouseEvent event) {

    red_col = Color.BLACK;

    repaint();

}

  };

  addMouseListener(drawing1);

  MouseMotionListener drawing2 = new MouseMotionListener() {

@Override

public void mouseDragged(MouseEvent event) {

    endX = event.getX();

    endY = event.getY();

    repaint();

}

@Override

public void mouseMoved(MouseEvent event) {

}

  };

  addMouseMotionListener(drawing2);

    }

    @Override
    public void paint(Graphics graphic) {

  super.paint(graphic);

  graphic.setColor(red_col);

  graphic.drawLine(startX, startY, endX, endY);
    }

    public static void main(String args[]) {

  JFrame jFrame = new MouseListenerExample();

  jFrame.setSize(600, 400);

  jFrame.setVisible(true);
    }
}

 
This was an example on how to use MouseListener in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button