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="" xmlns="" fx:controller="accordionproject.FXMLDocumentController">
        <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="">
                <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" />

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.

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
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.

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