Search code examples
javafxgridpane

GridPane stay centered while window is resized


Currently my gridpane stays centred on the X axis while the window is resized, how can I get it to stay centred on the Y axis as well?

I have this in a tab in a TabPane:

    GridPane grid = new GridPane();
    grid.setPadding(new Insets(20, 0, 0, 0));
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(20);
    grid.setVgap(20);

    this.setContent(grid);

Solution

  • The key here is VBox.setVgrow(tabPane, Priority.ALWAYS);

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.Priority;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    /**
     *
     * @author blj0011
     */
    public class JavaFXApplication65 extends Application
    {
    
        @Override
        public void start(Stage primaryStage)
        {
            VBox root = new VBox();
            MenuBar mb = new MenuBar();
    
            TabPane tabPane = new TabPane();
            root.getChildren().addAll(mb, tabPane);
    
    
            //StackPane stackPane = new StackPane();
            CreateProfilePane cp = new CreateProfilePane();
            tabPane.getTabs().add(cp);
            VBox.setVgrow(tabPane, Priority.ALWAYS);
    
    
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
    }
    

    enter image description here

    enter image description here