JScrollPane in JScrollPane

旧街凉风 提交于 2019-12-01 09:59:17

问题


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?


回答1:


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);
    }
});



回答2:


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



来源:https://stackoverflow.com/questions/8729629/jscrollpane-in-jscrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!