How do I handle mouse event in Swing?

package org.kodejava.example.swing;

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

public class MouseListenerDemo extends JFrame {
    public MouseListenerDemo() {
        initComponents();
    }

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

    private void initComponents() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 400);
        JTextArea textArea = new JTextArea();
        textArea.setText("Press the mouse button...");

        MouseAdapter mouseAdapter = new MyMouseAdapter();
        textArea.addMouseListener(mouseAdapter);
        getContentPane().add(textArea);
    }

    private static class MyMouseAdapter extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            System.out.println("MouseListenerDemo.mouseClicked");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("MouseListenerDemo.mousePressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("MouseListenerDemo.mouseReleased");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("MouseListenerDemo.mouseEntered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("MouseListenerDemo.mouseExited");
        }
    }
}

Leave a Reply

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