Adding space between buttons in VBox


I have a collection of buttons:

VBox menuButtons = new VBox();
menuButtons.getChildren().addAll(addButton, editButton, exitButton);

I want to add some spacing between this buttons, without using a CSS sheet. I think there should be a way for this.

setPadding(); is for the Buttons in the Box. setMargin(); should be for the Box itself. But I didn't find a way for the spacing between the buttons.

I'm glad for any ideas. :)

4/27/2017 6:45:12 AM

Accepted Answer

VBox supports spacing:

VBox menuButtons = new VBox(5);


3/2/2017 1:52:57 PM

Just call setSpacing method and pass some value. Example with HBox (it's same for VBox):

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.stage.Stage;

public class SpacingDemo extends Application {
    public static void main(String[] args) {

    public void start(Stage stage) {
        stage.setTitle("Spacing demo");

        Button btnSave = new Button("Save");
        Button btnDelete = new Button("Delete");
        HBox hBox = HBoxBuilder.create()
                .spacing(30.0) //In case you are using HBoxBuilder
                .padding(new Insets(5, 5, 5, 5))
                .children(btnSave, btnDelete)

        hBox.setSpacing(30.0); //In your case

        stage.setScene(new Scene(hBox, 320, 240));;

And this is how it looks:

Without of spacing:

enter image description here

With spacing:

enter image description here

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