How can I add padding to StackPane using hbox

I'm still learning how to mess around with panels and hbox,vbox etc.. I want to be able to set paddings around the center of StackPane.

    public void start(Stage stage) throws Exception {

        HBox hbox = new HBox(10); // number sets spacing between things
        Button roll1 = new Button("Roll");
        hbox.setPadding(new Insets(40,40,40,40));
        StackPane root = new StackPane();
        root.setAlignment(hbox, Pos.CENTER);

        Scene scene = new Scene(root, 500, 500);
public static void main(String[] args) {

I've tried using hbox to set padding around roll(which is a button) so that it will have padding of 40,40,40,40 when it's called(?) in StackPane.

My code sets padding around the button, but it is not positioned in center even though I've done

root.setAlignment(hbox, Pos.CENTER);

Read more here:

Content Attribution

This content was originally published by Rae Leng at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: