【问题标题】:JavaFX TabPane : Why is the content not refreshed after selecting different tab?JavaFX TabPane:为什么选择不同的选项卡后内容不刷新?
【发布时间】:2016-06-10 02:10:46
【问题描述】:

所以我目前是第一次在 JavaFx 中使用 tabpane。

在它没有按预期工作之后,我一次减少了一点 tabpanes 的内容,直到它看起来像这样:

<ScrollPane prefWidth="Infinity" prefHeight="Infinity" fitToWidth="true" xmlns:fx="http://javafx.com/fxml"
                            fx:controller="de.Cos4U.ui.scene.control.CosplayView">
    <VBox alignment="top_left" minWidth="200" prefHeight="200" spacing="5">

        <TabPane prefHeight="Infinity" minHeight="Infinity">
            <tabs>
                <Tab text="Tab1">
                    <content>
                        <Label text="test"/>
                    </content>
                </Tab>
                <Tab text="Tab2">
                    <content>
                        <Label text="tests2"/>
                    </content>
                </Tab>
            </tabs>
        </TabPane>
    </VBox>
</ScrollPane>

选择“Tab2”后,内容区仍显示“test”标签。滚动时它会立即刷新。就是这么简单的tabpane,为什么不刷新内容呢?

编辑:我现在使用的是 1.8u74。我编辑了 fxml,所以现在它尽可能完整,但尽可能减少。 控制器不会以任何方式影响选项卡窗格。到目前为止,它处理简单的用户输入。删除此示例不需要的任何内容后,将其减少为 0。

【问题讨论】:

  • 发布完整的 FXML(和控制器,如果需要)以提供 MCVE。请包括重现行为所需的部分。 “当你滚动时” - 用什么? TabPanes 正常更换他们的Tabs,你发布的是标准的TabPane
  • 另外发布您正在使用的 JavaFX 版本:我在使用 1.8u60 版本时遇到了一些令人耳目一新的问题。
  • 我使用的是 1.8u74。
  • 我现在使用了一个非常肮脏的解决方法。 Whenever a tab is selected i programmatically increase and decrease the stage-width a little bit.这会强制重新绘制。
  • @Shiniji Kudo 你好,我也有同样的问题,但当我向 TabPane 添加新标签时,如果我不稍微调整窗口大小,则不会显示 :(

标签: javafx tabpanel


【解决方案1】:

我以前也有同样的问题。我不知道它没有正确更新的确切原因,但如果你想强制刷新布局,你可以请求它而不是使用诸如更改大小之类的解决方法。

requestLayout();

假设您想在控制器中设置选项卡的内容。

class Panel extends TabPane {
    private Tab tab;

    public void setTabContent(Node container) {
        tab.setContent(container);
        getSelectionModel().select(tab);
        requestLayout(); // to force refresh the layout
    }
}

然后你从你的控制器调用这个方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2011-01-30
    • 2017-01-28
    • 2013-01-02
    相关资源
    最近更新 更多