Oak
Oak

Reputation: 518

Detecting Mouse Wheel Stops

I am building a simple program and I need to get boolean false when the user's mouse wheel becomes stopped after having been scrolling. My code is below with some more information.

public class WheelHandler extends MouseAdapter {

    public void mouseWheelMoved(MouseWheelEvent e) {
        moved = e.getWheelRotation();
        scrolling = true;
    }

    scrolling = false; // How do I make this run when the wheel becomes stopped?
}

Upvotes: 0

Views: 1743

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

Rolling is a series of discrete movements, so you're going to have to decide how much time between discrete movements means "stopped", and then use a Swing Timer to check for this time increment. You will need to decide on some arbitrary time slice (below it's int TIMER_DELAY = 100 or 100 msec), start some timer when a user moves the mouse wheel, and cancel the same timer when they use the wheel again before the slice is up.

For example:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class NotifyWheelStopped extends JPanel {
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;
    private static final String WHEEL_STOPPED = "Wheel has stopped";
    private static final String WHEEL_MOVING = "Wheel is moving";
    public static final int TIMER_DELAY = 100;
    private JLabel notificationLabel = new JLabel(WHEEL_STOPPED, SwingConstants.CENTER);
    private WheelHandler wheelHandler = new WheelHandler();
    private Timer wheelMovementTimer;

    public NotifyWheelStopped() {
        add(notificationLabel);
        addMouseWheelListener(wheelHandler);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class WheelHandler extends MouseAdapter {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            notificationLabel.setText(WHEEL_MOVING);
            if (wheelMovementTimer != null && wheelMovementTimer.isRunning()) {
                wheelMovementTimer.stop();
            }
            wheelMovementTimer = new Timer(TIMER_DELAY, new WheelMovementTimerActionListener());
            wheelMovementTimer.setRepeats(false);
            wheelMovementTimer.start();
        }
    }

    private class WheelMovementTimerActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            notificationLabel.setText(WHEEL_STOPPED);
        }
    }

    private static void createAndShowGui() {
        NotifyWheelStopped mainPanel = new NotifyWheelStopped();

        JFrame frame = new JFrame("NotifyWheelStopped");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

Even with a delay of 100 msec, the notification can be jumpy when the wheel is turned slowly.

Upvotes: 2

Related Questions