Search code examples
javafxautosizevbox

JavaFX - ScrollPane/VBox - Auto shrink


I'm developing a JavaFX application for kids to control a little robot, the kid would define the commands throught Drag-n-Drops command blocks. The target area is a Scrollpane with a VBox.

I would like to know if there is any way to make the ScrollPane and VBox autoresizable, thus when I add a node to the Green block it must grow (aldeady doing) and when I remove a node from Green block it must shrink.

Here is the code:

// Set to BorderPane#Center<br/>
boxCode = new VBox(0.0);
boxCode.setMinSize(400, 80);
// here I also set MaxSize using Double.MAX_VALUE and USE_COMPUTED_SIZE

paneCode = new ScrollPane(boxCode);
paneCode.setMinSize(400, 80);
paneCode.setStyle("-fx-background-color:transparent;");
paneCode.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
layout.setCenter(paneCode);

VBox with the nodes:
enter image description here

VBox without the nodes:
enter image description here

Here is my project: GitHub - Programming Block

Thank you!


Solution

  • I'm not really sure if I understand the problem. When I set minHeight for VBox and ScrollPane like in the example below it works for me.

     public void start(Stage primaryStage) {
    
            Button btnAdd = new Button("add");
            Button btnRemove = new Button("remove");
            VBox box = new VBox(btnAdd, btnRemove);
            box.setStyle("-fx-border-color:red");
    
            btnAdd.setOnAction(e -> box.getChildren().add(new Label("test")));
    
            btnRemove.setOnAction(e -> {
                int idx = box.getChildren().size();
                box.getChildren().remove(idx - 1);
            });
    
            ScrollPane scrollPane = new ScrollPane(box);
            scrollPane.setMinHeight(200);
            box.setMinHeight(200) ;
    
            BorderPane root = new BorderPane(scrollPane);
    
            primaryStage.setScene(new Scene(root, 300, 300));
            primaryStage.show();
        }