1
votes

I am writing a program using JavaFX in which I have

Borderpane -> Center -> VBox -> ScrollPane-> Gridpane

I would like to be able to add as many rows to the gridpane as I like and have the scrollpane automatically extend itself.

Note: I have some very long files so I only included the important bits. If you need the whole FXML or Controller file please leave me a comment.

Here is the FXML for my gridpane/scrollpane:

<BorderPane>        
<center>
<VBox>
<children>
<ScrollPane fx:id="messageScrollPane" vbarPolicy="ALWAYS" hbarPolicy="NEVER" hmax="0.0" vmax="0.0">
<content>
<GridPane fx:id="messageBox" prefHeight="540.0" prefWidth="589.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
<RowConstraints maxHeight="Infinity" minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
</rowConstraints>
</GridPane>
</content>
</ScrollPane>
</children>
</VBox>
</center>
</BorderPane>

I have the following code inside of my Controller for the FXML

  @FXML
private GridPane messageBox;

 if (me.getMail().size() > 0) {
                                for (int i = 0; i < me.getMail().size(); i++) {
                                    messageBox.add(new Label(me.getMail().get(i).toString()), 0, rowNum);
                                    rowNum++;
                                }
                            }

Thank you all for your help!

Ryan

1
I wrote a simple description about the ScrollPane in the Documentation, try to use it, (hoping it will help you), hereBo Halim

1 Answers

0
votes

You have placed the ScrollPane inside a VBox. You should set the vgrow priority for the ScrollPane to Priority.ALWAYS to allow the ScrollPane to grow to fill the available area of the VBox.

You can do this in FXML by adding the following attribute:

VBox.vgrow="ALWAYS"

to the <ScrollPane VBox.vgrow="ALWAYS" ...> element

Alternatively, you can do it in code via:

VBox.setVgrow(scrollPane, Priority.ALWAYS);

Also, a separate issue with your code is that you are adding rows dynamically (in a loop in code) to your grid pane. If you wish to do that, you should define RowConstraints in your FXML (as FXML cannot contain a variable number of rows). Instead, apply row constraints within your loop:

for (int i = 0; i < me.getMail().size(); i++) {
    messageBox.add(new Label(me.getMail().get(i).toString()), 0, rowNum);
    rowNum++;
    messageBox.getRowConstraints().add(new RowConstraints(...));
}