15

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.

Reuben Sanders
  • 370
  • 1
  • 2
  • 11
  • 3
    In HBox & VBox, setPadding defines the gap between each node, but in BorderPane only defines the gap between the BorderPane and its Parent, so my guess is that you will have to use BorderPane.setMargin for each one of the nodes inside it – Suicide Platypus Aug 18 '15 at 22:55
  • 1
    In HBox and VBox you call setSpacing() to define the gap between elements. setPadding() on BorderPane and Vbox/HBox works exactly the same. – Foumpie Aug 31 '17 at 13:11

2 Answers2

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

Community
  • 1
  • 1
Foumpie
  • 1,465
  • 1
  • 14
  • 10
-4

It's possible to set the padding:

In JFX:

<BorderPane>
   <padding>
      <Insets top="10" right="10" bottom="10" left="10"/>
   </padding>
</BorderPane>

In code:

@FXML
private BorderPane borderPane;    
...
this.borderPane.setPadding(new Insets(10, 10, 10, 10));
Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139