Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding the MouseWheelListener in Swing

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"?

like image 430
maloney Avatar asked Nov 05 '12 10:11

maloney


People also ask

Do I need a mouse wheel listener for a scroll pane?

Usually it is not necessary to implement a mouse-wheel listener because the mouse wheel is used primarily for scrolling. Scroll panes automatically register mouse-wheel listeners that react to the mouse wheel appropriately.

How do I change the direction of mouse wheel events?

Rotate the mouse wheel in the opposite direction. You will see mouse-wheel events in the down direction. Try changing your mouse wheel's scrolling behavior your system's mouse control panel to see how the output changes.

How to programmatically detect whether the mouse is equipped with mouse wheel?

There is no way to programmatically detect whether the mouse is equipped with a mouse wheel. Alternatively, use the corresponding MouseAdapter AWT class, which implements the MouseWheelListener interface, to create a MouseWheelEvent and override the methods for the specific events.

How to interpret the value of the mouse wheel rotation?

If the mouse wheel rotated towards the user (down) the value is positive. If the mouse wheel rotated away from the user (up) the value is negative. Returns the number of units that should be scrolled per notch.


Video Answer


1 Answers

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;
    }
};
like image 121
kleopatra Avatar answered Nov 15 '22 13:11

kleopatra