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:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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