How do I handle mouse wheel event?

Category: javax.swing, viewed: 6640 time(s).
package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;

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

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 200);

        JTextArea textArea = new JTextArea();
        textArea.addMouseWheelListener(new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent e) {
                System.out.println("MouseWheelListenerDemo.mouseWheelMoved");

                //
                // If wheel rotation value is a negative it means rotate up, while
                // positive value means rotate down
                //
                if (e.getWheelRotation() < 0) {
                    System.out.println("Rotated Up... " + e.getWheelRotation());
                } else {
                    System.out.println("Rotated Down... " + e.getWheelRotation());
                }

                //
                // Get scrolled unit amount
                //
                System.out.println("ScrollAmount: " + e.getScrollAmount());

                //
                // WHEEL_UNIT_SCROLL representing scroll by unit such as the
                // arrow keys. WHEEL_BLOCK_SCROLL representing scroll by block
                // such as the page-up or page-down key.
                //
                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) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MouseWheelListenerDemo().setVisible(true);
            }
        });
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats