event
MouseMotionListener example
With this tutorial we shall show you how to work with the MouseMotionListener
interface in Java. It is a very useful feature when you want to have full control over mouse events and mouse input that the users give. It also particularly useful when you want to make your application behave according to the mouse events that occur. This is very important as it’s very easy for the user to make use of the mouse in order to provide input for your application.
In short in order to work with MouseMotionListener
, one should follow these steps:
- Create a class that implements the
MouseMotionListener
- Override
mouseMoved
,mouseDragged
methods in order to customize the handling of these specific event. Now every time the user moves the mouse o drags an object, the corresponding method will be executed.
Let’s take a look at the code snippets that follow.
package com.javacodegeeks.snippets.desktop; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class MouseMotionEvent extends JPanel implements MouseMotionListener { JTextArea text; GreenArea blankSpace; static final String nl = "n"; public MouseMotionEvent() { super(new GridBagLayout()); GridBagLayout grid = (GridBagLayout) getLayout(); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.weightx = 2.0; constraints.weighty = 2.0; constraints.insets = new Insets(2, 2, 2, 2); blankSpace = new GreenArea(new Color(0.18f, 0.45f, 0.25f)); grid.setConstraints(blankSpace, constraints); add(blankSpace); constraints.insets = new Insets(0, 0, 0, 0); text = new JTextArea(); text.setEditable(false); JScrollPane scrPane = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrPane.setPreferredSize(new Dimension(190, 85)); grid.setConstraints(scrPane, constraints); add(scrPane); blankSpace.addMouseMotionListener(this); addMouseMotionListener(this); setPreferredSize(new Dimension(630, 630)); setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30)); } @Override public void mouseMoved(MouseEvent event) { saySomething("Mouse moved", event); } @Override public void mouseDragged(MouseEvent event) { saySomething("Mouse dragged", event); } void saySomething(String eventDescription, MouseEvent e) { text.append(eventDescription + " (" + e.getX() + "," + e.getY() + ")" + " pointing at " + e.getComponent().getClass().getName() + nl); text.setCaretPosition(text.getDocument().getLength()); } private static void dispGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame jFrame = new JFrame("SwingMouseMotionEventDemo"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new MouseMotionEvent(); newContentPane.setOpaque(true); jFrame.setContentPane(newContentPane); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { dispGUI(); } }); } } class GreenArea extends JLabel { Dimension minSize = new Dimension(100, 100); public GreenArea(Color c) { setBackground(c); setOpaque(true); setBorder(BorderFactory.createLineBorder(Color.black)); } @Override public Dimension getMinimumSize() { return minSize; } @Override public Dimension getPreferredSize() { return minSize; } }
This was an example on how to work with MouseMotionListener.