ciprianr
ciprianr

Reputation: 309

JavaFX - How can i add a container in an anchor pane

I have a simple project that has a fxml with a splitter.

So the fxml is this:

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="accordionproject.FXMLDocumentController">
    <children>
        <SplitPane fx:id="splitPane" dividerPositions="0.29797979797979796" focusTraversable="true" layoutX="60.0" layoutY="14.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns:fx="http://javafx.com/fxml">
            <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
            </items>
        </SplitPane>
    </children>
</AnchorPane>

What i would want is to insert a vbox in the left anchor pane from the splitter using only java code.

Can this be done?

I am new to fxml so any help would be apreciated.

Thank you in advance.

Upvotes: 3

Views: 19701

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40298

Add an fx:id to the AnchorPane you want to manipulate:

<AnchorPane fx:id="leftAnchorPane" minHeight="0.0" minWidth="0.0"
    prefHeight="160.0" prefWidth="100.0" />

Get it in your controller as a @FXML member field:

public class FXMLDocumentController
{
    @FXML private AnchorPane leftAnchorPane;
    ...
}

And manipulate it in the desired place (initialize() shown here, can be -almost- anywhere else):

public void initialize() {
    VBox vbox = new VBox();
    ...
    AnchorPane.setTopAnchor(vbox, 10.0); // obviously provide your own constraints
    leftAnchorPane.getChildren().add(vbox);
}

Upvotes: 12

Related Questions