Handle mouse motion event : MouseMotionListener « Swing Event « Java Tutorial






import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main extends JFrame {
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);

    JTextArea textArea = new JTextArea("drag it...");
    textArea.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        System.out.println("Mouse Dragged...");
      }

      public void mouseMoved(MouseEvent e) {
        System.out.println("Mouse Moved...");
      }
    });

    getContentPane().add(textArea);
  }

  public static void main(String[] args) {
    new Main().setVisible(true);
  }
}








15.30.MouseMotionListener
15.30.1.The MouseMotionListener Interface
15.30.2.Demonstrates mouse motion eventsDemonstrates mouse motion events
15.30.3.How to Write a Mouse-Motion ListenerHow to Write a Mouse-Motion Listener
15.30.4.Handle mouse motion event