How do I handle mouse motion event?

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class MouseMotionListenerDemo extends JFrame {
    public MouseMotionListenerDemo() {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MouseMotionListenerDemo().setVisible(true));

    private void initComponents() {
        setSize(500, 500);

        JTextArea textArea = new JTextArea("Hello World... try to move the mouse, click and 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...");


Leave a Reply

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