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


Question

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.

1
3
4/15/2016 6:59:50 AM

Accepted Answer

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);
}
12
12/23/2013 9:32:16 AM

You should try the JavaFX Scene Builder. It makes editing your fxml much easier as it is all done graphically.

http://www.oracle.com/technetwork/java/javafx/tools/index.html


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon