Home » Desktop Java » awt » event » MouseWheelListener example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

MouseWheelListener example

With this tutorial we are going to show you how to use a MouseWheelListener in Java. This event listener is particularly useful when you want to add extra user friendly functionality in your application. For example, if you want the user to change the value of a specific item, you can allow him to do that using the wheel of the mouse.

In short in order to work with MouseWheelListener in Java you can:

  • Create a new MouseWheelListener
  • Override the mouseWheelMoved method, which will fire up every time the user moves the mouse wheel.
  • Use addMouseWheelListener method to bundle a specific component with the listener. Every time the cursor is in the area of the component and the wheel is moved the listener will handle the event as we said before.

Let’s see the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

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

import javax.swing.JFrame;

public class MouseWheelListenerExample extends JFrame {

    private static final Color colorArray[] = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN,

  Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};

    public MouseWheelListenerExample() {

  super();

  final Container cPane = getContentPane();

  MouseWheelListener mouseWheelListener = new MouseWheelListener() {

int cnt;

private static final int up_color = 1;

private static final int down_col = 2;

@Override

public void mouseWheelMoved(MouseWheelEvent event) {

    int stps = event.getWheelRotation();

    int dir = (Math.abs(stps) > 0) ? up_color : down_col;

    changeBackground(dir);

}

private void changeBackground(int dir) {

    cPane.setBackground(colorArray[cnt]);

    if (dir == up_color) {

  cnt++;

    } else {

  --cnt;

    }

    if (cnt == colorArray.length) {

  cnt = 0;

    } else if (cnt < 0) {

  cnt = colorArray.length - 1;

    }

}

  };

  cPane.addMouseWheelListener(mouseWheelListener);
    }

    public static void main(String args[]) {

  JFrame jFrame = new MouseWheelListenerExample();

  jFrame.setSize(600, 400);

  jFrame.setVisible(true);
    }
}

This was an example no how to use MouseWheelListener in Java.

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
skymab Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
skymab
Guest
skymab

did you even test your code?

int dir = (Math.abs(stps) > 0) ? up_color : down_col;

Do you notice something? Something which has to do with Math.abs?