1
votes

I am relatively new to JavaFx and I need help in getting a Vbox to grow as nodes are added to it.

I've place a VBox in a ScrollPane. The VBox gets filled with TitledPanes as they come in. But once the TitledPanes fill the space allotted the Vbox, the TitledPanes begin to overlap. Ideally I would want to Vbox to resize itself and use the ScrollPane to navigate.

I have the Vbox Max Height set to USE_COMPUTED_SIZE. I've added a listener to the ScrollPane to listen to changes in size of the VBox but no luck. Any Suggestions?

scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
    public void changed(ObservableValue<? extends Number> ov,
            Number old_val, Number new_val) {
        vBox.setLayoutY(-new_val.doubleValue());
    }
});
1
Please provide an MCVE to make us able to reproduce. Normally (if you do not use any constraints) if you put a VBox inside a ScrollPane the VBox starts to grow as more and more children are added and the ScrollPane displays its scroll bar.DVarga

1 Answers

0
votes

Is there any reason you are using a VBox rather than an Accordion? With an Accordion you could use a ListChangeListener to check for changes in TitledPane number and then adjust the size of the Accordion if an item was added or removed:

accordion.getPanes().addListener(new ListChangeListener<Item>() {
     public void onChanged(Change<tem> c) {
         while (c.next()) {
             if(c.wasAdded() || c.wasRemoved){
                 //resize Accordion in dependency of the vvalue of your scrollpane
             }
         }
     });

For a VBox, the principle should still be valid:

vbox.getChildren().addListener(new ListChangeListener<Item>() {
     public void onChanged(Change<tem> c) {
         while (c.next()) {
             if(c.wasAdded() || c.wasRemoved){
                 //rezise VBox in dependency of the vvalue of your scrollpane
             }
         }
     });

based on