BorderPane spacing between nodes

Reuben Sanders picture Reuben Sanders · Aug 18, 2015 · Viewed 12.6k times · Source

Is it possible to set a spacing between nodes on a BorderPane? The Swing-equivalent would be hgap and vgap on BorderLayout.

I didn't find anything in the documentation and the only viable workaround I can think of would be to selectively set margins on the child nodes to replicate the effect.

Answer

Foumpie picture Foumpie · Aug 31, 2017
Insets insets = new Insets(10);
BorderPane bp = new BorderPane();
    
Node topNode = new Label("TOP");
bp.setTop(topNode);
BorderPane.setMargin(topNode, insets);

Node centerNode = new Label("CENTER");
bp.setCenter(centerNode);
BorderPane.setMargin(centerNode, insets);

Node bottomNode = new Label("BOTTOM");
bp.setBottom(bottomNode);
BorderPane.setMargin(bottomNode, insets);

Be aware: this will put a spacing of 20 (10 from top and 10 from center) between top and center. Similar for the spacing between center and bottom.

The documentation

public static void setMargin(Node child, Insets value)

Sets the margin for the child when contained by a border pane. If set, the border pane will lay it out with the margin space around it. Setting the value to null will remove the constraint.