加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JTextPane阻止在父JScrollPane中滚动

发布时间:2020-12-15 05:03:56 所属栏目:Java 来源:网络整理
导读:我有以下“树”的对象: JPanel JScrollPane JPanel JPanel JScrollPane JTextPane 当使用鼠标滚轮滚动外部JScrollPane我遇到一个恼人的问题.一旦鼠标光标触及内部JScrollPane,似乎滚动事件就会传递到JScrollPane中,并且第一个不再处理.这意味着滚动“父”JS
我有以下“树”的对象:

JPanel
    JScrollPane
        JPanel
            JPanel
                JScrollPane
                    JTextPane

当使用鼠标滚轮滚动外部JScrollPane我遇到一个恼人的问题.一旦鼠标光标触及内部JScrollPane,似乎滚动事件就会传递到JScrollPane中,并且第一个不再处理.这意味着滚动“父”JScrollPane停止.

是否可以仅禁用内部JScrollPane上的鼠标滚轮?或者更好的是,如果没有要滚动的内容(大多数情况下textpane只包含1-3行文本),则禁用滚动,但如果有更多内容则启用它?

解决方法

我也遇到了这个恼人的问题,而Sbodd的解决方案对我来说是不可接受的,因为我需要能够在表格和JTextAreas中滚动.我希望该行为与浏览器相同,其中鼠标悬停在可滚动控件上将滚动该控件直到控件达到最低点,然后继续滚动父滚动窗格,通常是整个页面的滚动窗格.

这个课就是这样做的.只需使用它代替常规的JScrollPane即可.我希望它对你有所帮助.

/**
 * 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());
    }
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读