Question

I have this code, but nothing happens. I don't know why the degreesSlider.getValue() + 1 does not work. I would be thankful for any suggestions.

degreesSlider.addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation();
        if (notches < 0) {
            System.out.println("Mouse wheel moved UP " + -notches + " notch(es)");
            degreesSlider.setValue(degreesSlider.getValue() + 1);
        } else {
            System.out.println("Mouse wheel moved DOWN " + notches + " notch(es)");
            degreesSlider.setValue(degreesSlider.getValue() - 1);
        }
    }
});
Was it helpful?

Solution

Try running the code from the Swing tutorial on How to Write a Mouse Wheel Listener. When I ran that code is appear that the "notch" only changes for every 3 units of wheel movement. Therefore when you scroll up you actually get 3 down scrolls for every up scroll and the slider slowly moves towards 0.

As a quick fix I just did:

    if (notches < 0) {
        System.out.println("Mouse wheel moved UP " + -notches + " notch(es)");
        slider.setValue(slider.getValue() + 1);
    } else
    if (notches > 0) {
        System.out.println("Mouse wheel moved DOWN " + notches + " notch(es)");
        slider.setValue(slider.getValue() - 1);
    }

OTHER TIPS

Here is the code Demo for moving JSlider with mouse wheel rotation. I hope this would help you to handle mouse wheel rotation for JSlider more effectively.

import javax.swing.*;
import java.awt.event.*;
public class JSliderDemo extends JFrame implements MouseWheelListener
{
    JSlider degreesSlider;
    public void prepareAndShowGUI()
    {
        degreesSlider = new JSlider(0,100);
        degreesSlider.setMajorTickSpacing(2);
        degreesSlider.setMinorTickSpacing(1);
        degreesSlider.setPaintLabels(true);
        degreesSlider.setPaintTicks(true);
        degreesSlider.setPaintTrack(true);
        degreesSlider.addMouseWheelListener(this);
        getContentPane().add(degreesSlider);
        setSize(1000,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    @Override
    public void mouseWheelMoved(MouseWheelEvent evt)
    {
        if (evt.getWheelRotation() < 0 )//mouse wheel was rotated up/away from the user
        {
            int iNewValue = degreesSlider.getValue() - degreesSlider.getMinorTickSpacing()  ;
            if (iNewValue >= degreesSlider.getMinimum())
            {
                degreesSlider.setValue(iNewValue);
            }
            else
            {
                degreesSlider.setValue(0);
            }
        }
        else
        {
            int iNewValue = degreesSlider.getValue() + degreesSlider.getMinorTickSpacing()  ;
            if (iNewValue <= degreesSlider.getMaximum())
            {
                degreesSlider.setValue(iNewValue);
            }
            else
            {
                degreesSlider.setValue(degreesSlider.getMaximum());
            }
        }
    }
    public static void main(String[] st)
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                JSliderDemo jsd = new JSliderDemo();
                jsd.prepareAndShowGUI();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top