[英]JavaFx: auto resize TitledPane
我在垂直定向的SplitPane
有两个TitledPane
的视图。 我希望当我折叠其中一个时,将另一个调整为Scene
的高度。 这是我的.fxml
文件的代码:
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:id="pane"
fx:controller="stackoverflow.three.Controller">
<center>
<SplitPane fx:id="split" orientation="VERTICAL">
<TitledPane fx:id="first" text="First">
<TableView>
<columns>
<TableColumn text="Test"/>
</columns>
</TableView>
</TitledPane>
<TitledPane fx:id="second" text="Second">
<TableView>
<columns>
<TableColumn text="Test"/>
</columns>
</TableView>
</TitledPane>
</SplitPane>
</center>
</BorderPane>
如您所见,如果我折叠第一个视图,则视图的底部有一个缝隙,但我不希望有此缝隙。
我试图将maxHeight
例如设置为Infinity,但随后自动向上移动到第一个却不起作用...任何想法我该怎么办?
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<BorderPane fx:id="pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="stackoverflow.three.Controller">
<center>
<VBox prefHeight="800.0">
<children>
<TitledPane fx:id="first" text="First">
<content>
<TableView prefHeight="2000.0">
<columns>
<TableColumn text="Test" />
</columns>
</TableView>
</content>
</TitledPane>
<TitledPane fx:id="second" text="Second">
<content>
<TableView prefHeight="2000.0">
<columns>
<TableColumn text="Test" />
</columns>
</TableView>
</content>
</TitledPane>
</children>
</VBox>
</center>
</BorderPane>
我遇到了类似的问题,并通过将两个TitledPanes的maxHeight设置为MAX_VALUE来解决了该问题。 在我的情况下,不需要侦听器和包装器(vbox,...)。 我正在使用openjfx 11.0.1。
好,这是计划B:
使用带有SplitPane的fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<BorderPane fx:id="pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="stackoverflow.three.Controller">
<center>
<SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="800.0">
<items>
<TitledPane fx:id="first" text="First">
<content>
<TableView prefHeight="2000.0">
<columns>
<TableColumn text="Test" />
</columns>
</TableView>
</content>
</TitledPane>
<TitledPane fx:id="second" text="Second">
<content>
<TableView prefHeight="2000.0">
<columns>
<TableColumn text="Test" />
</columns>
</TableView>
</content>
</TitledPane>
</items>
</SplitPane>
</center>
</BorderPane>
向两个TitledPane对象添加侦听器到它们的boolean expanded
属性
expanded.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
resizeSplitPane();
}
});
private void resizeSplitPane(){
// reposition split pane divider here depending on the status of both
// TableViews
if(!tableView1.isExpanded() && tableView2.isExpanded()){
splitPane.setDividerPosition(0, 0.1);
}else if(tableView1.isExpanded() && !tableView2.isExpanded()){
splitPane.setDividerPosition(0, 0.9);
}else{
splitPane.setDividerPosition(0, 0.5);
}
}
我根据这里的答案设法弄清楚了一些东西: 动画分割窗格分隔线
titledPane.expandedProperty().addListener((observable, oldValue, newValue) ->
{
double target = d1Orig;
if( !newValue )
{
d1Orig = splitPane.getDividers().get(0);
target = 0.0;
}
KeyValue keyValue = new KeyValue(splitPane.getDividers().get(0).positionProperty(), target);
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(500), keyValue));
timeline.play();
});
d1Orig是一个double值,用于在折叠前保存原始的拆分窗格位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.