2

I have a JScrollPane, which has a JPanel for its content pane. To that JPanel I add smaller JPanels, and as expected, if I add too much JPanel, a vertical scrollbar will appear.

The problem is, that my small JPanels contains a JScrollPane too for a JEditorPane. I'd like to use the mouse wheel only for the outer scrollpane, not for the smaller scrollpane. I already set wheelScrollingEnabled() to false for the small scrollpane, but if I scroll in any direction, and the mouse gets over the JEditorPane, the scrolling doesn't work anymore.

Any advice?

Howard
  • 38,639
  • 9
  • 64
  • 83
Rothens
  • 755
  • 7
  • 18
  • +1 for the interesting catch: my expectation would have been that setting the wheelScrollEnabled to false would do the trick :-) – kleopatra Jan 05 '12 at 12:04

2 Answers2

8

You may try to forward the wheel events from the inner scroll pane to its parent.

innerScrollPane.addMouseWheelListener(new MouseWheelListener() {

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        innerScrollPane.getParent().dispatchEvent(e);
    }
});
Howard
  • 38,639
  • 9
  • 64
  • 83
  • good dirty workaround :-) The behaviour (of not reverting to dispatching the wheelEvent to the ancestor on wheelScrollEnabled == false) is a bug in BasicScrollPaneUI, IMO - it should add/remove the internally installed wheelListener as appropriate – kleopatra Jan 05 '12 at 12:00
1

Add a MouseWheelListener to your JScrollPane and when handling an event, pass it to the main JScrollPane by invoking its dispatchEvent(AWTEvent) method;

Vlad
  • 1,723
  • 12
  • 16
  • no, unnecessarily hard-coding dependencies is not an option, especially not as the editorPane has nothing to do with the problem – kleopatra Jan 05 '12 at 12:04
  • Agree, it the listener should be registered with the JScrollPane. – Vlad Jan 05 '12 at 12:06
  • better, but still hard-coding a dependency from inner to main ;-) look at @Howard's solution for a hard-coded free approach – kleopatra Jan 05 '12 at 17:26
  • Yeh, I know, that's why I upvoted @Howard's answer... I just corrected my answer so that it at least not wrong. – Vlad Jan 05 '12 at 18:28