0

This is getting me quite confused: I have a small application that uses a JSpinner for numeric values. I added a MouseAdapter to it, setting the value depending on the resulting event's getScrollAmount() and getWheelRotation().

Everything is working quite fine as long as the application is run on linux (Debian Wheezy, Oracle JDK 1.6.0_32). That is, scrolling the mouse wheel while the JSpinner has focus works just fine (using GTK LookAndFeel).

Testing the software on Microsoft Windows exposed a different behavior: The JSpinner will not react on mouse wheel movement. LookAndFeels don't seem to be a problem here, as i already tried MetalLookAndFeel on both Linux and Windows.

Any suggestions on how to get this JSpinner reacting on MouseWheelEvents on both OSes?

Thank you.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DrGlitch
  • 136
  • 11

1 Answers1

1

Hmm im not sure what could be wrong, but here is code designed on windows 7 and it works fine so maybe try it out on yours and see where the code is different:

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 *
 * @author David
 */
public class JSpinnerMouse extends JFrame {

    private JSpinner jspinner;

    public JSpinnerMouse() {
        createAndShowUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JSpinnerMouse jSpinnerMouse = new JSpinnerMouse();
            }
        });
    }

    private void createAndShowUI() {
        setTitle("JSpinner using mouse wheel");
        setSize(300, 300);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        addComponentsToContentPane(getContentPane());
        addListeners();
        setVisible(true);
    }

    private void addComponentsToContentPane(Container contentPane) {
        jspinner = new JSpinner();
        contentPane.add(jspinner);
    }

    private void addListeners() {
        this.addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent mwe) {
                if (MouseWheelEvent.WHEEL_UNIT_SCROLL == mwe.getScrollType()) {
                    int value = (int) jspinner.getValue();
                    if (mwe.getWheelRotation() == -1) {//up
                        jspinner.setValue(value + 1);
                    } else {//down
                        jspinner.setValue(value - 1);
                    }
                }
            }
        });
    }
}

Also to mention, I could not find the getDirection() method in the MouseWheelEvent like you said you are using so i used the getUnitsToScroll() which will return either a positive or negative value depending on the direction. Maybe this is whats causing the trouble. Where did you find a getDirection() method in the MouseWheelEvent Class?

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • sorry for calling it `getDirection()`, actually I did use `getWheelRotation()` to get the direction. I will try out your suggestion ASAP, this appears to be just the thing! – DrGlitch Jun 15 '12 at 23:46
  • oh i see, well i edited the above code to fit in with what you have done and it works on my windows perfectly – David Kroukamp Jun 15 '12 at 23:51
  • Thank you for your assistance! This did the trick and it works perfectly on Windows XP, Windows 7 and Debian GNU/Linux. I still wonder why `getWheelRotation()` failed me on Windows. – DrGlitch Jun 16 '12 at 00:25
  • Okay, it appears it wasn't `getWheelRotation()`'s fault after all: Your updated code is working fine for my Windowses as well. Conclusion: Problem lies elsewhere, nevertheless, you made the point. Thank you. – DrGlitch Jun 16 '12 at 00:34