Java – jtextpane prevents scrolling in the parent JScrollPane

I have the following "tree" objects:

JPanel
    JScrollPane
        JPanel
            JPanel
                JScrollPane
                    JTextPane

When using the mouse wheel to scroll the external JScrollPane, I encountered an annoying problem Once the mouse cursor touches the internal JScrollPane, it seems that the scrolling event will be passed to the JScrollPane and the first one will no longer be processed This means that scrolling the "parent" JScrollPane stops

Can I disable only the mouse wheel on the internal JScrollPane? Or better, disable scrolling if there is nothing to scroll (textpane contains only 1-3 lines of text in most cases), but enable it if there is more content?

Solution

I also encountered this annoying problem, and sbodd's solution is unacceptable to me because I need to be able to scroll in tables and jtextareas I want this behavior to be the same as in browsers, where the mouse hovers over a scrollable control, scrolls the control until the control reaches its lowest point, and then continues to scroll the parent scrolling pane, usually the scrolling pane of the entire page

That's what this class does Just use it instead of the regular JScrollPane I hope it will help you

/**
 * A JScrollPane that will bubble a mouse wheel scroll event to the parent 
 * JScrollPane if one exists when this scrollpane either tops out or bottoms out.
 */
public class PDControlScrollPane extends JScrollPane {

public PDControlScrollPane() {
    super();

    addMouseWheelListener(new PDMouseWheelListener());
}

class PDMouseWheelListener implements MouseWheelListener {

    private JScrollBar bar;
    private int prevIoUsValue = 0;
    private JScrollPane parentScrollPane; 

    private JScrollPane getParentScrollPane() {
        if (parentScrollPane == null) {
            Component parent = getParent();
            while (!(parent instanceof JScrollPane) && parent != null) {
                parent = parent.getParent();
            }
            parentScrollPane = (JScrollPane)parent;
        }
        return parentScrollPane;
    }

    public PDMouseWheelListener() {
        bar = PDControlScrollPane.this.getVerticalScrollBar();
    }
    public void mouseWheelMoved(MouseWheelEvent e) {
        JScrollPane parent = getParentScrollPane();
        if (parent != null) {
            /*
             * Only dispatch if we have reached top/bottom on prevIoUs scroll
             */
            if (e.getWheelRotation() < 0) {
                if (bar.getValue() == 0 && prevIoUsValue == 0) {
                    parent.dispatchEvent(cloneEvent(e));
                }
            } else {
                if (bar.getValue() == getMax() && prevIoUsValue == getMax()) {
                    parent.dispatchEvent(cloneEvent(e));
                }
            }
            prevIoUsValue = bar.getValue();
        }
        /* 
         * If parent scrollpane doesn't exist,remove this as a listener.
         * We have to defer this till Now (vs doing it in constructor) 
         * because in the constructor this item has no parent yet.
         */
        else {
            PDControlScrollPane.this.removeMouseWheelListener(this);
        }
    }
    private int getMax() {
        return bar.getMaximum() - bar.getVisibleAmount();
    }
    private MouseWheelEvent cloneEvent(MouseWheelEvent e) {
        return new MouseWheelEvent(getParentScrollPane(),e.getID(),e
                .getWhen(),e.getModifiers(),1,e
                .getClickCount(),false,e.getScrollType(),e
                .getScrollAmount(),e.getWheelRotation());
    }
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>