Swing MouseMotion Adapter



Swing Mouse Motion Adapter

  1. MouseMotionAdapter class is an abstract adapter class in Java Swing.
  2. MouseMotionAdapter class is for receiving mouse motion events.
  3. Methods specified in MouseMotionAdapter class are empty.
  4. MouseMotionAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.MouseMotionAdapter class –

public abstract class MouseMotionAdapter
   extends Object
      implements MouseMotionListener

Class constructors

MouseMotionAdapter()

Class methods

No. Method Return Type Parameter Invoked when
1 mouseDragged() void MouseEvent Mouse button is pressed on a component and then dragged.
2 mouseMoved() void MouseEvent Mouse cursor has been moved onto a component but no buttons is clicked

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

WindowAdapter Example

AdapterExample.java

package com.c4learn;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showMouseApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showMouseApapter() {
      headLabel.setText("MouseMotionAdapter Example");

      JPanel panel = new JPanel();
      panel.setBackground(Color.gray);
      panel.setLayout(new FlowLayout());
      
      panel.addMouseMotionListener(new MouseMotionAdapter() {
         public void mouseMoved(MouseEvent e) {
            msgLabel.setText("Mouse Moved: (" + e.getX() + ", "
                  + e.getY() + ")");
         }
      });

      JLabel msglabel = new JLabel("www.c4learn.com");
      panel.add(msglabel);

      controlPanel.add(panel);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

mouse_motion_adapter