0

I am making a Login Screen with a number pad, and I can't seem to center align a GridPane of buttons in a Pane. What am I doing wrong?

Main.java

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.input.KeyCombination;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

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

    public void start(Stage primaryStage) throws Exception{

        Rectangle2D bounds = Screen.getPrimary().getBounds();
        LoginScreen loginScreen = new LoginScreen(bounds.getWidth(), bounds.getHeight());

        Scene scene = new Scene(loginScreen.get());

        primaryStage.setScene(scene);
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        primaryStage.show();
        primaryStage.setFullScreen(true);
    }
}

LoginScreen.java

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;

public class LoginScreen {
    private Pane root;
    private GridPane numberPad;

    public LoginScreen(double screenWidth, double screenHeight){
        root = new Pane();
        root.setPrefSize(screenWidth, screenHeight);
        root.setBackground(new Background(new BackgroundFill(Color.AQUA, CornerRadii.EMPTY, Insets.EMPTY)));

        numberPad = new GridPane();

        Button button01 = new Button("1");
        Button button02 = new Button("2");
        Button button03 = new Button("3");
        Button button04 = new Button("4");
        Button button05 = new Button("5");
        Button button06 = new Button("6");
        Button button07 = new Button("7");
        Button button08 = new Button("8");
        Button button09 = new Button("9");

        numberPad.setAlignment(Pos.CENTER);

        numberPad.add(button01, 0, 0);
        numberPad.add(button02, 1, 0);
        numberPad.add(button03, 2, 0);
        numberPad.add(button04, 0, 1);
        numberPad.add(button05, 1, 1);
        numberPad.add(button06, 2, 1);
        numberPad.add(button07, 0, 2);
        numberPad.add(button08, 1, 2);
        numberPad.add(button09, 2, 2);

        root.getChildren().addAll(numberPad);

    }

    public Pane get(){
        return root;
    }
}

GUI code is verbose, and this post editor isn't letting me post my question as is, so I need these extra lines to get it to accept my question. If I thought I could cut down my code to just the numberPad.setAlignment(Pos.Center) and still make it clear how I am attempting to center my GridPane I most certainly would. I do humbly thank those who might lend me their time to help me solve this issue I have.

Edit 01:

My issue is that the GridPane itself is drawn in the top left corner of the screen rather than in the center of the screen.

DAIRAV
  • 723
  • 1
  • 9
  • 31
Jack J
  • 1,514
  • 3
  • 20
  • 28

1 Answers1

3

You need to actually set the alignment for the parent container. A Pane is not a valid container for doing this, however.

If you were to use a VBox instead, you could simply set its alignment as so:

VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);

That will cause all of the children of the VBox to be placed in the center.

The Pos enum also provides other methods of positioning, including TOP_CENTER, TOP_LEFT, and BOTTOM_RIGHT, for example.

Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • I may be mistaken, but I believe CollumnConstraints are used to align the elements within the GridPane, and not align the GridPane itself. If I use this code, the Hgap and Vgap are visible, but the GridPane still is drawn in the top left corner of the screen. – Jack J Aug 01 '18 at 00:41
  • You would need to set the alignment for the parent of the GridPane. For example, place the GridPane in a VBox and use `pane.setAlignment(Pos.CENTER)` – Zephyr Aug 01 '18 at 00:42
  • I have updated my answer to actually answer your real question. :) – Zephyr Aug 01 '18 at 00:46