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.

@Override
    public void start(Stage stage) throws Exception {

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

        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show();  
        
    }
public static void main(String[] args) {
        launch(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: https://stackoverflow.com/questions/64949201/how-can-i-add-padding-to-stackpane-using-hbox

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: