How do I handle mouse motion event?

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;

public class MouseMotionListenerDemo extends JFrame {
    public MouseMotionListenerDemo() {

    protected void initComponents() {
        setSize(400, 400);

        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...");


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


Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

Leave a Reply