7

I want to disable scrolling with the mousewheel in my JScrollPane while ctrl is pressed. When you press ctrl and move the wheel you will zoom in/out AND also scroll the panel, which is not what I wanted.

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 resolved it myself. If you have any tweaks go ahead and tell me!

2 Answers2

2

Take a look at Mouse Wheel Controller. You won't be able to use the exact code but you should be able to use the concept of the class.

The code replaces the default MouseWheelListener with a custom listener. Then it recreates the event with one different parameter in redispatches the event to the default listeners.

In your case you won't need to create a new event you will just need to prevent any event with a Control modifier from being redispatched to the default listeners and instead you invoke the code you posted in your question.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Not that usefull, I edited my original code and solved my problem without using a new class. – user1076625 Dec 02 '11 at 15:11
  • @user1076625 if so, post that as an answer,and accept that.Appending answer to the question is not the right way. Cheers! – COD3BOY Dec 02 '11 at 16:06
  • @user1076625, Your code makes lots of assumptions. You assume there is only ever one MouseWheelListener. You assume you never use any of the default functionality of the first listener. If that is your requirement then fine, but don't say the suggestion was not useful, because it demonstrates a better way to remove only specific functionality from all potential listeners. Also, it looks like you copied my suggestion to remove the listeners. the only difference is you didn't add them back in. – camickr Dec 02 '11 at 16:25
1

In order to temporarily disable scrolling you could manipulate the scrollbar's unit increment value and, respectively, restore it again.

Just add a key listener to the view port panel and react to Ctrl key pressed:

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);
  }
});
PAX
  • 1,056
  • 15
  • 33