Search code examples
javafxfxmlsplitpane

Remove child from parent created in FXML


I have a series of nested Split Panes created in an FXML file. I have buttons which set the max size of their children to zero, effectively hiding them.

This leaves the Split Panes divider there, and causes issues when re-sizing as the Split Panes are nested and the dividers are colliding.

What would work is removing the child node from the split pane, causing it to have only one remaining node, and not drawing the extra divider.

The issue is that I can't access getChildren() for the SplitPanes, as their protected. Split panes as defined in 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>

Controller code I want to use:

splitPane2.getChildren().remove(viewA);

Any ideas?


Solution

  • For a SplitPane, getChildren() is not really what you want anyway; it will produce a list of all the nodes contained in the SplitPane: not just the actual content but the representation of the dividers (and maybe other stuff) as well.

    The getItems() method gives a list of all the nodes that are displayed between the dividers. So all you need is

    splitPane2.getItems().remove(viewA);