Example usage for java.awt Container addMouseWheelListener

List of usage examples for java.awt Container addMouseWheelListener

Introduction

In this page you can find the example usage for java.awt Container addMouseWheelListener.

Prototype

public synchronized void addMouseWheelListener(MouseWheelListener l) 

Source Link

Document

Adds the specified mouse wheel listener to receive mouse wheel events from this component.

Usage

From source file:MouseWheelTest.java

public MouseWheelTest() {
    super();//from  www  . ja  va  2  s  .com
    final Container contentPane = getContentPane();

    MouseWheelListener listener = new MouseWheelListener() {
        int colorCounter;

        private static final int UP = 1;

        private static final int DOWN = 2;

        public void mouseWheelMoved(MouseWheelEvent e) {
            int count = e.getWheelRotation();
            int direction = (Math.abs(count) > 0) ? UP : DOWN;
            changeBackground(direction);
        }

        private void changeBackground(int direction) {
            contentPane.setBackground(colors[colorCounter]);

            if (direction == UP) {
                colorCounter++;
            } else {
                --colorCounter;
            }

            if (colorCounter == colors.length) {
                colorCounter = 0;
            } else if (colorCounter < 0) {
                colorCounter = colors.length - 1;
            }
        }
    };
    contentPane.addMouseWheelListener(listener);
}