Question

I have VBox filled with ImageViews and VBox is placed within BorderPane on CENTER.
Now when move ScrollBar I get its value and I want my VBox to be relocated by Y axis as much as ScrollBar value.
My code is simple:

@Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("MyScrollbarSample");

        ScrollBar scrollBar = new ScrollBar();
        scrollBar.setBlockIncrement(10);
        scrollBar.setMax(180);
        scrollBar.setOrientation(Orientation.VERTICAL);
        // scrollBar.setPrefHeight(180);
        scrollBar.setValue(90);

        final VBox vBox = new VBox(1);
        // vBox.setPrefHeight(180);
        for (int i = 1; i < 6; i++) {
            vBox.getChildren().add(new ImageView(new Image(getClass().getResourceAsStream("fw" + i + ".jpg"))));
        }

        scrollBar.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                System.out.println("newValue=" + newValue.doubleValue());
//              vBox.setLayoutY(-newValue.doubleValue());
                vBox.relocate(vBox.getLayoutX(), - newValue.doubleValue());
            }
        });

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(vBox);
        borderPane.setRight(scrollBar);
        stage.setScene(new Scene(borderPane));
        stage.show();
    }

Unfortunately neither setLayoutY nor relocate on VBox does not move it when I move ScrollBar.

No correct solution

OTHER TIPS

It sounds like you are trying to make a vbox that floats with the scrollbar. Instead of using a listener, you could try binding the scrollbar value property to the appropriate vbox layout dimension. E.g.

ScrollBar bar = new ScrollBar();
VBox box = new VBox();

box.layoutYProperty().bind(bar.valueProperty());

However, if you are just placing the VBox in the center of the borderpane, then this may not work depending on how you have the VBox placed in the borderpane. You may need to wrap the VBox in an AnchorPane so that you can do absolute positioning like this.

I am not 100% sure this will work, but you might want to give it a try. Good luck.

  • chooks
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top