Search code examples
javajavafxfxmlborderpane

Can I set a BorderPane element's width in FXML?


It seems like I can't set any properties on the <left> StackPane. I'd like to set it's width to 200, for example.

<BorderPane fx:id="borderPane" fx:controller="main.Controller"
            xmlns:fx="http://javafx.com/fxml" prefHeight="600.0" prefWidth="800.0">

    <left prefWidth="200"> // This doesn't work
        <ListView fx:id="listView"/>
    </left>
    <right>
        <ImageView fx:id="staticImage"/>
    </right>

</BorderPane>

I know that I can do this programatically:

StackPane left = new StackPane();
left.setPrefWidth(200);
borderPane.setLeft(left);

But for purposes of my project I simply cannot do this that way. Are there alternatives?


Solution

  • You can just translate the Java code you posted directly to FXML:

    <BorderPane fx:id="borderPane" fx:controller="main.Controller"
                xmlns:fx="http://javafx.com/fxml" prefHeight="600.0" prefWidth="800.0">
    
        <left>
            <StackPane prefWidth="200" />
        </left>
        <right>
            <ImageView fx:id="staticImage"/>
        </right>
    
    </BorderPane>