Handle mouse wheel event : MouseWheelListener « Swing Event « Java Tutorial






import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

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

public class Main extends JFrame {
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 200);
    JTextArea textArea = new JTextArea();
    textArea.addMouseWheelListener(new MouseWheelListener() {
      public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getWheelRotation() < 0) {
          System.out.println("Up... " + e.getWheelRotation());
        } else {
          System.out.println("Down... " + e.getWheelRotation());
        }
        System.out.println("ScrollAmount: " + e.getScrollAmount());

        if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
          System.out.println("MouseWheelEvent.WHEEL_UNIT_SCROLL");
        }

        if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
          System.out.println("MouseWheelEvent.WHEEL_BLOCK_SCROLL");
        }
      }
    });

    getContentPane().add(textArea);
  }

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








15.31.MouseWheelListener
15.31.1.The MouseWheelListener Interface: defines a method to respond to the mouse wheel being rotated
15.31.2.How to Write a Mouse-Wheel ListenerHow to Write a Mouse-Wheel Listener
15.31.3.Handle mouse wheel event