0

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!

  • You just have to set the minWidth of the VBox appropriatly. vbox.setMinWidth(someValue) – jns May 05 '16 at 02:51
  • Sorry for forgot. I just edited to show the code and some more pictures. I used the ScenicView to inspect and the VBox actually is shrink to minsize but the ScrollPane isn't. It is keeping the last maximum size. – Wesley Egberto May 05 '16 at 03:09
  • Why do you need a `ScrollPane` if you don't plan to show the `ScrollBar`s? – fabian May 05 '16 at 09:07
  • I was showing, but then i decided to let it grow. I found the cause of this behavior. The left green bar is growing with the scrollpane/vbox, when I remove the line (`leftBackground.fitHeightProperty().bind(paneCode.heightProperty());`), the scrollpane/vbox started to shrink =) But the left green bar needs to grow together to keep the design. Thank you!!! – Wesley Egberto May 06 '16 at 01:06

1 Answers1

1

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();
    }
jns
  • 6,017
  • 2
  • 23
  • 28
  • After read you code, I cleaned the code to looks like your and then I found the problem. Thank you! Now I will try other way to make the left bar grow without break this behavior. – Wesley Egberto May 06 '16 at 01:08
  • Just for anyone try something like this, i used the following solution. [How to resize an image when resizing the window in JavaFx](http://stackoverflow.com/questions/22993550/how-to-resize-an-image-when-resizing-the-window-in-javafx) – Wesley Egberto May 06 '16 at 02:33