@Override protected void layoutChildren() { double layoutWidth = getLayoutBounds().getWidth(); double layoutHeight = getLayoutBounds().getHeight(); boolean vbarVisible = vbar.isVisible(); boolean hbarVisible = hbar.isVisible(); double vbarWidth = vbarVisible ? vbar.prefWidth(-1) : 0; double hbarHeight = hbarVisible ? hbar.prefHeight(-1) : 0; double w = layoutWidth - vbarWidth; double h = layoutHeight - hbarHeight; content.resize(w, h); hbar.setVisibleAmount(w); vbar.setVisibleAmount(h); if(vbarVisible) { vbar.resizeRelocate(layoutWidth - vbarWidth, 0, vbarWidth, h); } if(hbarVisible) { hbar.resizeRelocate(0, layoutHeight - hbarHeight, w, hbarHeight); } }
@Override public void start(Stage primaryStage) { ScrollBar scrollBar = new ScrollBar(); scrollBar.setOrientation(Orientation.VERTICAL); scrollBar.setMax(100); scrollBar.setVisibleAmount(50); scrollBar.valueProperty().addListener((a,b,c) -> System.out.println(c)); StackPane root = new StackPane(); root.getChildren().add(scrollBar); Scene scene = new Scene(root, 200, 500); // do layout root.applyCss(); root.layout(); ScrollBarMark mark1 = new ScrollBarMark(); ScrollBarMark mark2 = new ScrollBarMark(); mark1.attach(scrollBar); mark2.attach(scrollBar); mark1.setPosition(50); mark2.setPosition(75); primaryStage.setScene(scene); primaryStage.show(); }
scrollBar.setMax(TOTAL_WIDTH - canvas.getWidth()); scrollBar.setVisibleAmount(scrollBar.getMax() * canvas.getWidth() / TOTAL_WIDTH);