Java: JScrollPane disables scrolling when ctrl is pressed

I want to disable mouse scrolling in my JScrollPane while ctrl is pressed. When you press ctrl and move the wheel, you zoom in / out the AND image, and also scroll the panel, which I did not want.

Here's the working code:

    scroller = new JScrollPane(view);
    scroller.removeMouseWheelListener(scroller
            .getMouseWheelListeners()[0]);
    scroller.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(final MouseWheelEvent e) {
            if (e.isControlDown()) {
                if (e.getWheelRotation() < 0) {
                    // Zoom +
                } else {
                    // Zoom -
                }
            } else if (e.isShiftDown()) {
                // Horizontal scrolling
                Adjustable adj = getScroller().getHorizontalScrollBar();
                int scroll = e.getUnitsToScroll() * adj.getBlockIncrement();
                adj.setValue(adj.getValue() + scroll);
            } else {
                // Vertical scrolling
                Adjustable adj = getScroller().getVerticalScrollBar();
                int scroll = e.getUnitsToScroll() * adj.getBlockIncrement();
                adj.setValue(adj.getValue() + scroll);
            }
        }
    });

Edited my question and decided it myself. If you have any settings, tell me!

+5
source share
2 answers

See Mouse Controller. . You cannot use the exact code, but you must be able to use the concept of a class.

MouseWheelListener . redispatches .

, , - Control , , .

+2

, , , .

Ctrl:

editorPane.addKeyListener(new KeyAdapter(){
  @Override
  public void keyPressed(KeyEvent e) {
    if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0) 
      getVerticalScrollBar().setUnitIncrement(0);
    else 
      getVerticalScrollBar().setUnitIncrement(15);
  }

  @Override
  public void keyReleased(KeyEvent e) {
    if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0) 
      getVerticalScrollBar().setUnitIncrement(0);
    else 
      getVerticalScrollBar().setUnitIncrement(15);
  }
});
0

All Articles