[英]JavaFX FXML: Expand ScrollPane to Fit height of its Content Pane Until it reahces its MaxHeight property
我有以下FXML視圖:
我希望ScrollPane 擴展其高度以匹配其child vbox's height
,直到達到某個高度[例如200px],然后停止擴展。
在過去的1個小時里,我一直在使用Min/Pref Viewport Height
和Min/Max/Pref Height
屬性來實現此目的,但是沒有任何結果。
這可能嗎?
編輯 :這是一個最小,完整和可驗證的示例:
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());
}
}
這似乎可行:
<ScrollPane fx:id="scrollPane" maxHeight="200.0" minViewportHeight="0.0" minHeight="0.0">
<VBox fx:id="_buttonsContainer" />
</ScrollPane>
默認情況下,滾動窗格及其視口似乎具有一些固定的正最小高度。
。ScrollPane.prefHeightProperty()結合(VBox.heightProperty());
然后添加ScrollPane.setMaxHeight(200); 嘗試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.