[英]JavaFX VGrow TabPane on content change
In my JavaFX application, I have a TabPane
in a TitledPane
. 在我的JavaFX应用程序中, TabPane
中有一个TitledPane
。 Within the TabPane
is a VBox
which includes a button that increases the number of elements in that VBox
(and as such, the height of the VBox
). 内的TabPane
是一个VBox
,其包括一个按钮,增加元件的数目在VBox
(并且同样地,所述的高度VBox
)。
However, I seem to be unable to get the Tab
/ TabPane
to resize when the VBox grows. 但是,当VBox增大时,我似乎无法使Tab
/ TabPane
调整大小。 (Removing the TabPane
and putting the VBox
in the TitledPane
directly leads to proper on-the-fly sizing.) However, changing the tab makes the TabPane
realize that it is too small and resize. (删除TabPane
并将VBox
放在TitledPane
直接导致即时调整大小。)但是,更改选项卡会使TabPane
意识到它太小且无法调整大小。
Below is a small example FXML that can reproduce this. 下面是一个可以重现此效果的小示例FXML。 Click the button in the first tab any number of times then switch tabs. 单击第一个选项卡中的按钮任意次,然后切换选项卡。 the TabPane
will then resize to fit the new amount of content. 然后, TabPane
将调整大小以适应新的内容量。
How can I make the TabPane
resize on the button action? 如何在按钮操作上调整TabPane
大小?
fxml: fxml:
<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FXMLController" prefHeight="400" prefWidth="700">
<children>
<TitledPane>
<TabPane>
<tabs>
<Tab>
<VBox fx:id="vBox">
<children>
<Button onAction="#addElement" />
</children>
</VBox>
</Tab>
<Tab />
</tabs>
</TabPane>
</TitledPane>
</children>
</AnchorPane>
controller: 控制器:
@FXML VBox vBox;
@FXML void addElement() {
vBox.getChildren().add(new Label("Hello World"));
}
(of course with proper imports that I've left out for brevity's sake) (当然,为了简洁起见,我省略了适当的导入)
To get the redraw to happen, you need to mark the TabPane
as "dirty" -- that it requires recalculation of size in the UI. 为了进行重绘,您需要将TabPane
标记为“脏”-它需要重新计算UI中的大小。
This is done via calling the method requestLayout()
on the TabPane
. 这是通过在TabPane
上调用requestLayout()
方法来完成的。
See the docs for TabPane
and its superclass Parent
参见TabPane
及其超类Parent
的文档
To make this example behave, add fx:id="tabPane"
to the <TabPane>
fxml node, then add the field @FXML TabPane tabPane;
为了使该示例fx:id="tabPane"
,请将fx:id="tabPane"
到<TabPane>
fxml节点,然后添加字段@FXML TabPane tabPane;
to the controller and the line tabPane.requestLayout()
to #addElement
. 到控制器,并将tabPane.requestLayout()
行tabPane.requestLayout()
到#addElement
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.