JavaFX ScrollPane - How to use one scrollbar to scroll 2 ScrollPanes?

风流意气都作罢 提交于 2019-12-11 02:45:52

问题


I have BorderPane with left and center part, both are ScrollPanes. How to scroll them with one scrollbar ( vertical ). Or how to get access to one of the ScrollBars ?


回答1:


you can bind scrollpane1(sp1) vScrollBar property and set the changed value to other scrollpane vScrollbar property.

Sample code : this code automatically changes SP2 vScrollbar position when Sp1 vScrollbar position chnaged.

DoubleProperty vPosition = new SimpleDoubleProperty();
    vPosition.bind(sp1.vvalueProperty());
    vPosition.addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue arg0, Object arg1, Object arg2) {
             sp2.setVvalue((double) arg2);

        }
    }); 

Hint to get one scrolll bar to scroll two Scroll panes : Define a vertical scroll bar and then hide ( may be set opcaity to Zero or something ..) vscrollbars for two Scrollpanes. and then bind to defined scrollbar changes and set that chnaged values to both scrollpanes vscrollbars like above.




回答2:


The answer of @invariant did not work for me. But the code written below worked out.

ScrollPane sp1 = new ScrollPane();
ScrollPane sp2 = new ScrollPane();
sp1.hvalueProperty().bindBidirectional(sp2.hvalueProperty());

With the binding two components to each other, they scroll together horizontally. It would work for vertical case.



来源:https://stackoverflow.com/questions/14895517/javafx-scrollpane-how-to-use-one-scrollbar-to-scroll-2-scrollpanes

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