2

Why doesn't this align my HBox in the middle? I have to put 2 buttons in my HBox and align the HBox in the middle of the screen so I get those 2 buttons right in the middle next to each otherl.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;



public class firstwindow extends Application {

Button CreateButton;
Button SeeButton;


public static void main (String[] args) {
    launch(args);

}

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

    //Setting title
    primaryStage.setTitle("Basketball stats tracker");

    //creating buttons
    CreateButton = new Button("Create");
    SeeButton = new Button("See");

    //Creating HBox     
    HBox centerButtons = new HBox(15);  //15 is the space between box elements
    centerButtons.getChildren().addAll(CreateButton, SeeButton);

    //creating layout
    StackPane layout = new StackPane();
    layout.getChildren().add(centerButtons);
    StackPane.setAlignment(centerButtons, Pos.CENTER);

    //creating scene
    Scene scene = new Scene(layout, 600, 400);

    //creating Stage 
    primaryStage.setScene(scene);
    primaryStage.show();

}
}

Can you please guys help me with that?

Giuliopime
  • 707
  • 4
  • 21
  • From [the HBox documentation](https://docs.oracle.com/javase/10/docs/api/javafx/scene/layout/HBox.html): “The alignment of the content is controlled by the alignment property, which defaults to Pos.TOP_LEFT.” – VGR Jul 09 '18 at 00:51
  • Its convention that Java class names have upper-case letters for each word; for example your class may be named as `FirstWindow`, rather than `firstwindow`. Also, note the instance variable names use camelCase and need to have `private` access - `CreateButton` needs to be `createButton`, etc. – prasad_ Jul 09 '18 at 03:38
  • Thank you, I was setting the alignment of the node (with StackPane.setAlignment) instead of aligning the whole HBox with setAlignment – Giuliopime Jul 10 '18 at 08:44

0 Answers0