问题
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