JavaFX SplitPane子组件动态调整尺寸问题求助
解决JavaFX SplitPane中TitledPane折叠时VBox高度不收缩的问题
问题根源
核心问题在于:
campaignInfoVBox作为垂直SplitPane的子节点,错误使用了VBox.vgrow="ALWAYS"(该属性仅对VBox内部子组件生效,对SplitPane的子节点无意义);- 嵌套的
campaignInfoSplitPane未正确设置填充VBox的属性,导致VBox无法随内部TitledPane的折叠收缩高度; - SplitPane的子节点需要明确配置
SplitPane.resizableWithParent以允许其随内容调整大小。
解决方案
修改FXML中的关键配置:
调整campaignInfoVBox属性:
- 移除无效的
VBox.vgrow="ALWAYS"; - 添加
SplitPane.resizableWithParent="true",允许SplitPane根据VBox的内容高度调整占比; - 确保
maxHeight="Infinity"(默认值,若有固定值需改为Infinity)。
- 移除无效的
配置嵌套的campaignInfoSplitPane:
- 添加
VBox.vgrow="ALWAYS",让它填充campaignInfoVBox的可用空间,确保VBox高度由内部内容决定。
- 添加
配置testInfoScrollPane:
- 添加
SplitPane.resizableWithParent="true",允许它自动扩展填充SplitPane腾出的空间。
- 添加
修改后的完整FXML代码
<SplitPane fx:id="infoSplitPane" orientation="VERTICAL" dividerPositions="0.35"> <items> <VBox fx:id="campaignInfoVBox" spacing="10" maxHeight="Infinity" SplitPane.resizableWithParent="true"> <children> <Label text="Campaign Information" style="-fx-font-size: 120%" /> <SplitPane fx:id="campaignInfoSplitPane" orientation="VERTICAL" VBox.vgrow="ALWAYS"> <items> <TitledPane fx:id="campaignTestsFlow" text="Campaign Tests Flow"> <TableView fx:id="testCasesTableView" minHeight="52" maxHeight="Infinity" VBox.vgrow="ALWAYS"> <columnResizePolicy> <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> </columnResizePolicy> </TableView> </TitledPane> <TitledPane fx:id="campaignActionsInfo" text="Campaign Actions Information"> <SplitPane fx:id="campaignTablesSplitPane" orientation="VERTICAL" VBox.vgrow="ALWAYS"> <items> <RunnerActionsTreeTableView fx:id="campaignSetupTable" name="Campaign Setup" VBox.vgrow="SOMETIMES" /> <RunnerActionsTreeTableView fx:id="campaignTearDownTable" name="Campaign Tear Down" VBox.vgrow="ALWAYS" /> </items> </SplitPane> </TitledPane> </items> </SplitPane> </children> </VBox> <ScrollPane fx:id="testInfoScrollPane" fitToWidth="true" SplitPane.resizableWithParent="true"> <content> <VBox spacing="0" VBox.vgrow="ALWAYS"> <GridPane hgap="5.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" vgap="5.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="0.0"> <children> <VBox fx:id="titledPaneVbox" GridPane.rowIndex="13" GridPane.columnSpan="2"> <TitledPane fx:id="variablesTitlePane" maxHeight="1.7976931348623157E308" text="Variables Information" VBox.vgrow="ALWAYS" > <content> <TableView fx:id="variablesTable" maxHeight="Infinity" maxWidth="Infinity" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS" /> </content> </TitledPane> <TitledPane fx:id="eventsTitlePane" maxHeight="1.7976931348623157E308" text="Events Information" VBox.vgrow="ALWAYS" GridPane.columnSpan="2"> <content> <TableView fx:id="eventsTable" maxHeight="Infinity" maxWidth="Infinity" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS"/> </content> </TitledPane> </VBox> <Button fx:id="testCaseDirButton" text="Logs Folder" maxWidth="Infinity" GridPane.rowIndex="14" GridPane.hgrow="ALWAYS" GridPane.columnSpan="2" /> </children> </GridPane> </VBox> </content> </ScrollPane> </items> </SplitPane>
原理说明
SplitPane.resizableWithParent="true":告知SplitPane该子节点大小可随内容变化,SplitPane会自动调整分隔条位置适配子节点的首选高度;VBox.vgrow="ALWAYS"(用于嵌套SplitPane):确保内部SplitPane填充VBox的可用空间,让VBox高度完全由内部TitledPane的展开/折叠状态决定;- 移除无效的
VBox.vgrow="ALWAYS":该属性仅对VBox直接子组件生效,对作为SplitPane子节点的VBox无意义,反而会干扰布局计算。
内容的提问来源于stack exchange,提问作者Gilad Dahan
相关产品推荐
相关产品推荐

