JavaFX ScrollPane - How to use one scrollbar to sc

2019-07-16 14:15发布

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 ?

2条回答
在下西门庆
2楼-- · 2019-07-16 14:42

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.

查看更多
走好不送
3楼-- · 2019-07-16 14:46

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.

查看更多
登录 后发表回答