Overriding the MouseWheelListener in Swing
Asked Answered
M

2

12

I want to override the mouse wheel listener in Swing but only if they have the Control button pressed. The listener will be attached to a JPanel so that when they scroll the wheel it will scroll the JScrollPane and when they have the control button pressed and scroll the wheel it will zoom in. The default scroll of JScrollPane works (obviously) before I override it with my own listener. Here is my code:

mainPanel.addMouseWheelListener(new MouseWheelListener(){
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
            int notches = e.getWheelRotation();

            if (notches < 0) {
                redrawOnZoom(true);
            } else {
                redrawOnZoom(false);
            }
        }
    }
});

Is there a way of saying something like "If mouse is scrolled on its own then do default JScrollPane scrolling behaviour but If Ctrl is pressed then zoom"?

Maclean answered 5/11, 2012 at 10:33 Comment(5)
You may need (and I'm sorry for saying it) a KeyListener to monitor when the CRTL key is pressed and enable/disable the mouse wheel listenerTymothy
@Tymothy AWTListener returns bothHermanhermann
@user106... rather than updating the post with how you fixed it. either accept any relevant answer else use the Answer your own question to write how you fixed it. Do not update the question.Ethelethelbert
no, don't try to second-guess the default handling. Instead hand it over to the next party that might be interested (see my answer)Tensor
Ok I will revert to my question. ThanksMaclean
T
9

you can dispatch the event to its parent if you don't want to handle it:

final MouseWheelListener wheel = new MouseWheelListener() {

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        // handle some events here and dispatch others
        if (shouldHandleHere(e)) {
            LOG.info("do-my-own-stuff");
        } else {
            LOG.info("dispatch-to-parent");
            e.getComponent().getParent().dispatchEvent(e);
        } 
    }

    public boolean shouldHandleHere(MouseWheelEvent e) {
        return (e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0;
    }
};
Tensor answered 5/11, 2012 at 11:6 Comment(0)
E
0

Hint: Override the mouseWheelListener.

two functions : scroll() and other zoom.

Check for CTRL keyPress inside listener.

If pressed, call zoom() elsescroll()

refer tohow to write swing listener for guidence.

Ethelethelbert answered 5/11, 2012 at 10:37 Comment(3)
But how do you call swings mousewheellistener?Maclean
Thanks, Iv pretty much already got that far though - what I wanted to know was what basically needs to go into the the scroll() method? Like is there a way of calling swings default for scrolling?Maclean
@user1065547 , please check the provided link on details on how to scroll , RegardsEthelethelbert

© 2022 - 2024 — McMap. All rights reserved.