Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BorderPane spacing between nodes

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.

like image 271
Reuben Sanders Avatar asked Aug 18 '15 15:08

Reuben Sanders


People also ask

How to add spacing in JavaFX?

To add space around the inside perimeter of the layout pane, use the setPadding method. This method takes as a parameter an object of type Insets, which represents the size of the padding in pixels for the top, right, bottom, and left edge of an object.

What is BorderPane?

BorderPane lays out children in top, left, right, bottom, and center positions. The top and bottom children will be resized to their preferred heights and extend the width of the border pane. The left and right children will be resized to their preferred widths and extend the length between the top and bottom nodes.


1 Answers

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.

like image 93
Foumpie Avatar answered Dec 11 '22 16:12

Foumpie