【问题标题】:Remove child from parent created in FXML从 FXML 中创建的父级中删除子级
【发布时间】:2014-07-25 10:56:10
【问题描述】:

我在 FXML 文件中创建了一系列嵌套的拆分窗格。我有按钮可以将孩子的最大尺寸设置为零,从而有效地隐藏它们。

这会将拆分窗格分隔线留在那里,并在重新调整大小时导致问题,因为拆分窗格嵌套并且分隔线发生碰撞。

可行的方法是从拆分窗格中删除子节点,使其只剩下一个节点,并且不绘制额外的分隔线。

问题是我无法访问 SplitPanes 的 getChildren(),因为它们是受保护的。 fxml 中定义的拆分窗格

        <SplitPane fx:id="splitPane1" dividerPositions="0.75" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"  GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="2" GridPane.rowSpan="4" >
            <items>
                <SplitPane fx:id="splitPane2"  orientation="VERTICAL"  dividerPositions="0.4,0.8,1" >
                    <SplitPane fx:id="splitPane3"  orientation="HORIZONTAL" dividerPositions="0.5"  >
                        <items>
                            <Viewport fx:id="viewB" minWidth="0" minHeight="0"  />
                            <TilePane fx:id="view3D" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" />
                        </items>
                    </SplitPane>
                    <Viewport fx:id="viewC" minWidth="0" minHeight="0"  />
          <Viewport fx:id="viewA" minWidth="0" minHeight="0"  />
      </SplitPane>

我要使用的控制器代码:

splitPane2.getChildren().remove(viewA);

有什么想法吗?

【问题讨论】:

  • 您是否将控制器添加到您的 fxml 文件并尝试在您的控制器类中访问您的 SplitPane?
  • 是的,控制器添加到 FXML 的根目录,并试图从控制器类中访问。
  • 我可以获得孩子的只读列表,但不能进行任何更改。

标签: javafx fxml splitpane


【解决方案1】:

对于SplitPanegetChildren() 并不是你真正想要的;它将生成SplitPane 中包含的所有节点的列表:不仅是实际内容,还有分隔符的表示(可能还有其他内容)。

getItems() 方法给出了在分隔线之间显示的所有节点的列表。所以你只需要

splitPane2.getItems().remove(viewA);

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多