Javafx 基于子级的选项卡窗格动态调整大小
我有一个JavaFX层次结构,分解时基本上是这样的:Javafx 基于子级的选项卡窗格动态调整大小,javafx,javafx-8,Javafx,Javafx 8,我有一个JavaFX层次结构,分解时基本上是这样的: Parent // Tested with ScrollPane and AnchorPane, both work this way. | // The important part is that TabPane ISN'T the base Node | // So its sizing is under its own control, not the Window's | - TabPane | | - Ta
Parent // Tested with ScrollPane and AnchorPane, both work this way.
| // The important part is that TabPane ISN'T the base Node
| // So its sizing is under its own control, not the Window's
| - TabPane
| | - Tab
| | | - VBox
VBox的内容使其动态更改高度。不幸的是,选项卡窗格不会自动响应重新调整大小,后来的搜索显示,这似乎也与此有关
不幸的是,这里仍然存在一个问题,通过为选项卡窗格提供-fx背景色
,可以清楚地看出这一点。使用更改选项卡的手动/自动解决方案或调用TabPane#requestLayout
的我的解决方案,尽管TabPane增长正常,但无法收缩
(注意:如果您不告诉VBox使用maxHeight=“-Infinity”
(使用\u PREF\u SIZE
),VBox将是不减小大小的罪魁祸首。但是,使用此设置,VBox将收缩,使选项卡窗格大于其内容。)
揭示了我的测试用例(如下)的层次结构看起来更像这样:
AnchorPane
| - TabPane "tabPane"
| | - TabContentRegion
| | - TabContentRegion
| | | - VBox "vBox"
| | | | - Button
| | | | - Button
| | - TabHeaderArea
| | | - (Generated Stuff)
重新调整VBox的大小时,Scientive View确认VBox的布局边界
/边界父项
随大小的增加和减小而变化,但包含的TabContentRegion的边界
仅在VBox大于TabContentRegion时才会变化
TL;DR:是否有办法(解决方法或官方方法)使TabPane/TabContentRegion即使在重新调整子项大小时也能保持与其子项相同的维度(不占用额外空间)
,节选如下: FXML.FXML
<AnchorPane fx:controller="test.FXMLController"><children>
<TabPane fx:id="tabPane" style="-fx-background-color:#000000;"><tabs><Tab>
<VBox fx:id="vBox" maxHeight="-Infinity"><children>
<Button onAction="#addElement" />
<Button onAction="#remElement" />
</children></VBox>
</Tab></tabs></TabPane>
</children></AnchorPane>
测试用例使用TabPane#requestLayout
使TabPane增长,但提供了两个选项卡,以便您可以手动在它们之间切换,以确保TabPane不会收缩
此处,选项卡窗格位于滚动窗格中,一旦选项卡窗格超出滚动窗格的高度,即使它再次收缩,滚动窗格仍允许用户像以前一样向下滚动,因为选项卡窗格向下延伸。在上一个示例中,在滚动窗格中使用选项卡窗格,我做了以下更改: 向TableView添加一个ChangeListener,当pref高度发生更改时,它会通知选项卡窗格:
通过此更改,TabPane的高度将随着TableView的每次更改而更新,而TableView也会更新滚动窗格(滚动条将根据需要显示和删除)。使用绑定,它可以内联:
tabs.prefHeightProperty().bind(table.prefHeightProperty().add(70))代码>用于第二个示例。(选项卡窗格中应该有一个类似的VBox解决方案。)显然,add
部分将根据其他元素进行更改。我可以发誓我试过了,但我想不是。谢谢
@FXML VBox vBox;
@FXML TabPane tabPane;
@FXML void addElement() {
vBox.getChildren().add(0, new Label("Hello World"));
tabPane.requestLayout();
}
@FXML void remElement() {
if (vBox.getChildren().size() > 2) {
vBox.getChildren().remove(0);
tabPane.requestLayout(); // Doesn't seem to do anything
}
}
table.prefHeightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
tabs.setPrefHeight(table.getPrefHeight() + 75.0d);
}
});
@FXML
void addPerson() {
table.getItems().add(new Person());
// updateTabPaneSize(tabs);
}
@FXML
void remPerson() {
if (table.getItems().size() > 0) {
table.getItems().remove(0);
// updateTabPaneSize(tabs);
}
}