Toti
Toti

Reputation: 75

To make a pane scrollable in JavaFX

Can you please tell me, how can I make a pane scrollable? I am able to make an image scrollable, like this:

        VBox root = new VBox();
        ScrollPane sp = new ScrollPane();
        sp.setContent(new ImageView(new Image(getClass().getResourceAsStream("test.png"))));
        root.getChildren().add(sp);

But when I am trying to make a whole pane scrollable, which consists of some Buttons and a small animation, there happens nothing regarding scrollbars. There are no scrollbars. How can I solve that?

Thank you in advance.

Upvotes: 1

Views: 5480

Answers (1)

Peter Nehila
Peter Nehila

Reputation: 66

I tried to do it by myself, and it is because your scroll pane has fixed width and height, by resizing window, your scroll pane doesn't resize on itself. You can use anchor pane so scroll pane will resize with anchor pane. You could do something like this:

    ImageView image = new ImageView(new Image("wp1951596.jpg"));
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(image);
    scrollPane.setPrefSize(400, 400);
    AnchorPane.setTopAnchor(scrollPane, 0.);
    AnchorPane.setRightAnchor(scrollPane, 0.);
    AnchorPane.setBottomAnchor(scrollPane, 0.);
    AnchorPane.setLeftAnchor(scrollPane, 0.);
    rootPane.getChildren().add(scrollPane);

I would also recommend you using JavaFX with .fxml files. Then you can create your layout in SceneBuilder and it will make everything easier and faster.

Upvotes: 1

Related Questions