DarkMental
DarkMental

Reputation: 512

JavaFX FXML: Expand ScrollPane to Fit height of its Content Pane Until it reahces its MaxHeight property

I have the following FXML view:

I want the ScrollPane to expand its height to match its child vbox's height, until it reaches a certain height [200px for example] then it stops expanding.

I've been playing around for the past 1 hour with the Min/Pref Viewport Height, and Min/Max/Pref Height properties to achieve this, but nothing worked out.

Is this this possible to do?

Edit: Here's a minimal, complete, and verifiable example :

MainView.fxml

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="CENTER" prefHeight="0.0" prefWidth="200.0" spacing="5.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainController">
   <ScrollPane maxHeight="200.0"  VBox.vgrow="ALWAYS">
         <VBox fx:id="_buttonsContainer" />
   </ScrollPane>
   <Button fx:id="_btnAddButton" mnemonicParsing="false" text="Button" />
</VBox>

MainController.java

public class MainController implements Initializable{

    @FXML private Button _btnAddButton;
    @FXML private VBox   _buttonsContainer;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        _btnAddButton.setOnAction(e -> {
            addButton();
        });
    }

    private void addButton(){
        _buttonsContainer.getChildren().add(new Button());
    }
}

Upvotes: 1

Views: 1514

Answers (2)

James_D
James_D

Reputation: 209408

This seems to work:

   <ScrollPane fx:id="scrollPane" maxHeight="200.0" minViewportHeight="0.0" minHeight="0.0">
         <VBox fx:id="_buttonsContainer" />
   </ScrollPane>

The scroll pane, and its viewport, seem to have some fixed positive minimum height by default.

Upvotes: 2

Uthpala Bandara
Uthpala Bandara

Reputation: 47

ScrollPane.prefHeightProperty().bind(VBox.heightProperty());

then add ScrollPane.setMaxHeight(200); try this

Upvotes: 0

Related Questions