简体   繁体   English

JavaFX VGrow TabPane上的内容更改

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM