我有一个
SplitPane
,其中有两个项目,即VBox
和ScrollPane
。在 VBox
中,我有一些 TitledPane
孩子可以折叠,因此可以最小化 VBox
高度,或者这就是我认为会发生的情况。但问题是,只有 VBox
的内部元素被收缩,而实际的 VBox
子高度保持不变。
我希望
VBox
动态改变高度,并且 SplitPane
的分隔线自动填充缩小的 VBox
的间隙,以便 ScrollPane
能够填充该间隙。
<SplitPane fx:id="infoSplitPane" orientation="VERTICAL" dividerPositions="0.35">
<items>
<VBox fx:id="campaignInfoVBox" spacing="10" VBox.vgrow="ALWAYS">
<children>
<Label text="Campaign Information" style="-fx-font-size: 120%" />
<SplitPane fx:id="campaignInfoSplitPane" orientation="VERTICAL">
<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" VBox.vgrow="ALWAYS">
<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>
所以基本上我需要的是
testInfoScrollPane
在其 campaignInfoVBox
子级折叠时自动收缩时将其自身调整为 TitledPane
底部边距。
我相信您需要将
maxHeight
的 VBox
属性设置为其内容的 prefHeight。这样,VBox
就永远不会超出其内容的大小。
您可以尝试以下代码:
如果是java代码:
campaignInfoVBox.setMaxHeight(Region.USE_PREF_SIZE);
如果在 FXML 代码中:
<VBox fx:id="campaignInfoVBox" spacing="10" VBox.vgrow="ALWAYS" maxHeight="-Infinity">
下面是一个演示修复方法的快速演示:
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SplitPaneFitContentDemo extends Application {
@Override
public void start(final Stage stage) throws Exception {
StackPane content = new StackPane();
content.setMinSize(200, 800);
content.setStyle("-fx-background-color:linear-gradient(to bottom, yellow, red);");
ScrollPane scrollPane = new ScrollPane(content);
VBox pane = new VBox();
// Try setting the below one. In FXML, for the VBox it should have the attribute maxHeight="-Infinity"
pane.setMaxHeight(Region.USE_PREF_SIZE);
for (int i = 1; i < 3; i++) {
VBox tpContent = new VBox(10, new Button("Button A" + i), new Button("Button B" + i));
TitledPane tp = new TitledPane("Title " + i, tpContent);
tp.setMaxHeight(Double.POSITIVE_INFINITY);
VBox.setVgrow(tp, Priority.ALWAYS);
pane.getChildren().add(tp);
}
SplitPane splitPane = new SplitPane(pane, scrollPane);
splitPane.setOrientation(Orientation.VERTICAL);
splitPane.setDividerPositions(0.35);
StackPane root = new StackPane(splitPane);
Scene scene = new Scene(root, 300, 400);
stage.setScene(scene);
stage.setTitle("SplitPane");
stage.show();
}
}
更新: 当应用提供的 FXML 中的属性时,这就是它的工作原理。