关于内容更改的JavaFX VGrow TabPane [英] JavaFX VGrow TabPane on content change
问题描述
在我的JavaFX应用程序中,我在 TitledPane
中有 TabPane
。在 TabPane
中是 VBox
,其中包含一个增加中元素数量的按钮VBox
(因此, VBox的高度
)。
In my JavaFX application, I have a TabPane
in a 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
).
然而,当VBox增长时,我似乎无法获得 Tab
/ TabPane
来调整大小。 (删除 TabPane
并将 VBox
放入 TitledPane
直接导致适当的动态调整大小。)但是,更改选项卡会使 TabPane
意识到它太小并且调整大小。
However, I seem to be unable to get the Tab
/TabPane
to resize when the VBox grows. (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.
下面是一个可以重现这一点的小例子FXML。单击第一个选项卡中的按钮任意次,然后切换选项卡。 TabPane
然后将调整大小以适应新的内容量。
Below is a small example FXML that can reproduce this. 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?
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>
控制器:
@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)
推荐答案
要重绘,您需要标记 TabPane
作为脏 - 它需要在UI中重新计算大小。
To get the redraw to happen, you need to mark the TabPane
as "dirty" -- that it requires recalculation of size in the UI.
这是通过调用方法 requestLayout()
上的code>。
This is done via calling the method requestLayout()
on the TabPane
.
要使此示例运行,请将 fx:id =tabPane
添加到< TabPane>
fxml节点,然后将字段 @FXML TabPane tabPane;
添加到控制器和t他行 tabPane.requestLayout()
到 #addElement
。
To make this example behave, add fx:id="tabPane"
to the <TabPane>
fxml node, then add the field @FXML TabPane tabPane;
to the controller and the line tabPane.requestLayout()
to #addElement
.
这篇关于关于内容更改的JavaFX VGrow TabPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!