Home > Back-end >  Implementation of Scrollbar in JavaFX
Implementation of Scrollbar in JavaFX

Time:06-06

My problem is that my Maven-project (board game) take up quite a lot of size, and since my laptop is only 13 inches, I can not see the top and bottom part. I have tried many different lines of code that I found on the internet but it did not help. So my question is that; I have adjusted the size of my board game but I can only see that place inside the 400x400 dimension. So how can I add a scrollbar that can show the whole game by scrolling up and down, is it even possible?

I've tried to do something, it is commented out.

    Scene scene = this.stage.getScene();
    Parent root = scene.getRoot();
    ((BorderPane) root).setCenter(this.boardView);

    //ScrollPane sp = new ScrollPane();
    //sp.setContent(root);
    //Scene scene = this.stage.getScene(sp, 400, 400); //too many arguments..

    stage.setScene(scene);
    stage.centerOnScreen();
    appController.startGame();

I know I have to put the 'sp' in the code below (that's also the solutions I've found on the internet):

Scene scene = new Scene(sp, 400, 400);

But, how would I be able to do it, when I am creating the object like this?

Scene scene = this.stage.getScene();

How would I add Scrollingbar in my Window?

Or is there a completely different way to do it?

CodePudding user response:

A lot of the code you posted doesn't really make any sense. If you have

Scene scene = this.stage.getScene();

then obviously

stage.setScene(scene);

is completely pointless. Much of the rest of the code is not understandable either, at leasg without further context.

The basic answer to your question is that you wrap the boardView in a ScrollPane, and place the ScrollPane (instead of boardView) in the scene graph, wherever you need it. So something like

Scene scene = this.stage.getScene();
Parent root = scene.getRoot();
ScrollPane sp = new ScrollPane();
sp.setContent(this.boardView);
((BorderPane) root).setCenter(sp);
  • Related