event

Mouse wheel event example

With this tutorial we are going to sow 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, for example, 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 class that implements 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.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class WheelE extends JPanel implements MouseWheelListener {

    JTextArea jTextArea;
    JScrollPane scPane;
    final static String nl = "n";

    public WheelE() {

  super(new BorderLayout());

  jTextArea = new JTextArea();

  jTextArea.setEditable(false);

  scPane = new JScrollPane(jTextArea);

  scPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

  scPane.setPreferredSize(new Dimension(420, 350));

  add(scPane, BorderLayout.CENTER);

  jTextArea.append("");

  jTextArea.addMouseWheelListener(this);

  setPreferredSize(new Dimension(500, 500));

  setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent event) {

  String print;

  int steps = event.getWheelRotation();

  if (steps < 0) {

print = "Wheel moved UP " + -steps + " step(s)"

  + nl;

  } else {

print = "Wheel moved DOWN " + steps + " step(s)"

  + nl;

  }

  if (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {

print += "Type: WHEEL_UNIT_SCROLL" + nl;

print += "Amount: " + event.getScrollAmount()

  + " unit increments per step" + nl;

print += "Units to scroll: " + event.getUnitsToScroll()

  + " unit increments" + nl;

print += "Vertical unit increment: "

  + scPane.getVerticalScrollBar().getUnitIncrement(1)

  + " pixels" + nl;

  } else { //scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL

print += "Type: WHEEL_BLOCK_SCROLL" + nl;

print += "Vertical block increment: "

  + scPane.getVerticalScrollBar().getBlockIncrement(1)

  + " pixels" + nl;

  }

  saySomething(print, event);
    }

    void saySomething(String eventDescription, MouseWheelEvent e) {

  jTextArea.append(e.getComponent().getClass().getName() + ": "

    + eventDescription);

  jTextArea.setCaretPosition(jTextArea.getDocument().getLength());
    }

    private static void dispGUI() {

  JFrame.setDefaultLookAndFeelDecorated(true);

  JFrame frame = new JFrame("MouseWheelEventExample");

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JComponent newContentPane = new WheelE();

  newContentPane.setOpaque(true);

  frame.setContentPane(newContentPane);

  frame.pack();

  frame.setVisible(true);
    }

    public static void main(String[] args) {

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

    dispGUI();

}

  });
    }
}

This was an example on how to work MouseWheelListener in a Java Desktop application.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button