简体   繁体   English

Scrollpane不会调整大小以适应gridpane JavaFX

[英]Scrollpane won't resize to fit gridpane JavaFX

I am writing a program using JavaFX in which I have 我正在使用JavaFX编写程序

Borderpane -> Center -> VBox -> ScrollPane-> Gridpane 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. 如果您需要整个FXML或Controller文件,请给我留言。

Here is the FXML for my gridpane/scrollpane: 这是我的gridpane / scrollpane的FXML:

<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

  @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 瑞安

You have placed the ScrollPane inside a VBox . 您已将ScrollPane放在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. 您应该 ScrollPane 的vgrow优先级设置Priority.ALWAYS,以允许ScrollPane增长以填充VBox的可用区域。

You can do this in FXML by adding the following attribute: 您可以通过添加以下属性在FXML中执行此操作:

VBox.vgrow="ALWAYS"

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

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). 如果您希望这样做,您应该在FXML中定义RowConstraints (因为FXML不能包含可变数量的行)。 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(...));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM