Peter Penzov
Peter Penzov

Reputation: 1658

How to add ScrollPane to main stage

I'm interested how I can add ScrollPane to the main stage. I want when I reduce the size of the main page to use ScrollPane to move over the main stage. Is there any example?

Upvotes: 0

Views: 440

Answers (2)

Nick Rippe
Nick Rippe

Reputation: 6465

If your content of the ScrollPane is larger than the allowed size of the ScrollPane, the ScrollPane should automatically attach scrollbars. Here's an example:

public class JFXScroll extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        BorderPane main = new BorderPane();

        ScrollPane scroll = new ScrollPane();
        VBox box = new VBox();

        // Demo purposes; Wouldn't normally do this - just let the box automatically fit the content
        box.setPrefSize(1000, 500);
        box.setEffect(new ColorInput(0,0,1000,500,Color.LIME));

        scroll.setContent(box);

        main.setLeft(new Label("Left Content"));
        main.setCenter(scroll);

        Scene scene = new Scene(main, 300, 250);
        stage.setScene(scene);
        stage.show();


    }
    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 3

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17359

stage.setScene(new ScrollPane(yourContent)); should do it. Of course your content should have an appropriate min, max and preferred sizes

Upvotes: 0

Related Questions