[英]JavaFX FXML: Expand ScrollPane to Fit height of its Content Pane Until it reahces its MaxHeight property
I have the following FXML view: 我有以下FXML视图:
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. 我希望ScrollPane 扩展其高度以匹配其child vbox's height
,直到达到某个高度[例如200px],然后停止扩展。
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. 在过去的1个小时里,我一直在使用Min/Pref Viewport Height
和Min/Max/Pref Height
属性来实现此目的,但是没有任何结果。
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());
}
}
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. 默认情况下,滚动窗格及其视口似乎具有一些固定的正最小高度。
ScrollPane.prefHeightProperty().bind(VBox.heightProperty()); 。ScrollPane.prefHeightProperty()结合(VBox.heightProperty());
then add ScrollPane.setMaxHeight(200); 然后添加ScrollPane.setMaxHeight(200); try this 尝试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.