1

I'm not sure if this is possible and I couldn't find anything on it, but could I make a scrollpane scroll forever? I am constantly adding new buttons and labels into the window once a button is pressed, and eventually is gets to the bottom. I may be setting it up wrong but here is code:

BorderPane bp = new BorderPane();
GridPane gp = new GridPane();
Button b = new Button("click");
gp.add(b, 1, 1);
ScrollPane sp = new ScrollPane(gp);
bp.setTop(sp);
b.setOnAction(e -> createLabel());

1 Answers1

0

Your'e nearly there, all you now need to do is add the scroll pane as a child of whatever the container is

        GridPane gp = new GridPane();
        Button b = new Button("click");
        gp.add(b, 1, 1);
        b.setOnAction(e -> createLabel());

        ScrollPane sp = new ScrollPane(gp);

        container.add(sp); // where container is whatever node that'll contain the gridpane.

Play with this code

public class Controller {
    @FXML private VBox topLevelContainer; // root fxml element

    @FXML
    void initialize(){

        GridPane gridPane = new GridPane();
        ScrollPane sp = new ScrollPane(gridPane);
        topLevelContainer.getChildren().add(sp);

        // add a 100 buttons to 0th column
        for (int i = 0; i < 100; i++) {
            gridPane.add(new Button("button"),0,i);
        }


    }

}
Ryotsu
  • 786
  • 6
  • 16
  • Oh I forgot to add that inside my code. I have added it in my main container. The main container is a Border, and I have the gridpane within that one. The scrollbar will show up on the windows, but will only scroll down a little bit even if new labels are added off the screen. Thank you for responding though! –  Mar 10 '19 at 17:32
  • you need to encapsulate whatever parent node thats going to contain the buttons with the `ScrollPane` – Ryotsu Mar 12 '19 at 12:53
  • I will! I have not ever touches the controller part of this yet, so this should be exciting. Thank you, I'll let you know how it goes. –  Mar 12 '19 at 22:43
  • Cool! If it works, don't forget to accept the answer! – Ryotsu Mar 13 '19 at 02:27