javacodex.com
Java Examples
Java Examples
memu home questions

MouseWheelListener

A mouse wheel moved event is triggered if the wheel of the mouse is rotated when the component is in focus. If a mouse does not have a wheel, this event is not triggered.

Source: (MouseWheelEventExample.java)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class MouseWheelEventExample {
  public static void main(String [] args) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("MouseWheelEvent Example");
    frame.setSize(300,150);
 
    final JLabel label = new JLabel();
    frame.add(label);
 
    frame.addMouseWheelListener(new MouseWheelListener() {
        int count = 0;
        public void mouseWheelMoved(MouseWheelEvent mwe) { 
          count = count + mwe.getWheelRotation();
          label.setText("Moved: " + count );
        }
    });
 
    frame.setVisible(true);			
  }
}
 

Output:

$ java MouseWheelEventExample
Output



Contact: javacodex@yahoo.com