javauser-interfacejavafxlayoutborderpane

How to center my "inputPane" using JavaFX


How can I center my panel inputPane in a BorderPane, in JavaFX?

Here's my code.

public class MainStage {
    private Scene mainScene;
    private InputPane inputPane;
    private DisplayPane displayPane;
    private EditPane editPane;
    private BorderPane borderPane;

    private VBox mainPane;

    public MainStage(){
        borderPane = new BorderPane();
        inputPane=new InputPane();
        displayPane=new DisplayPane();
        editPane=new EditPane();


        mainPane=new VBox();
        mainPane.getChildren().addAll(inputPane.getPane(),displayPane.getTable(),editPane.getPane());
        mainPane.setSpacing(10);

        mainScene=new Scene(mainPane,800,480);
        mainScene.getStylesheets().add("styleSheet.css");
    }
    public Scene getMainScene() {
        return mainScene;
    }
    public InputPane getInputPane() {
        return inputPane;
    }
    public DisplayPane getDisplayPane() {
        return displayPane;
    }
    public EditPane getEditPane(){
        return editPane;
    }
}

I'm trying to modify InputPane class.

public class InputPane {

    private Text server;
    private Text textNumber;
    private TextField tfNumber;

    private Button addButton;
    private GridPane griglia;

    public InputPane() {

        textNumber=new Text("Insert Port Number: ");
        server = new Text("SERVER");
        server.setStyle("-fx-font-weight:bold;"
                       +"-fx-color: black;"
                       +"-fx-font-size: 18;");
        tfNumber = new TextField();
        tfNumber.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));

        griglia=new GridPane();

        griglia.add(server,1,0);
        griglia.add(textNumber,0,1);
        griglia.add(tfNumber,1,1);
        griglia.setVgap(5);
        griglia.setHgap(10);

        GridPane.setHalignment(server, HPos.CENTER);
        GridPane.setHalignment(textNumber, HPos.CENTER);

    }

    public GridPane getPane() {
        return griglia;
    }
    public Button getAddButton() {
        return addButton;
    }
}

I give you some ideas of how you can see now my GUI:

This is the result
This is the result

And I would like to make somethink like this
And I would like to make somethink like this

Thank you


Solution

  • Your question is not really clear as you ask :

    How can I center my panel inputPane in a BorderPane, in JavaFX?

    But in your example, you never use your BorderPane, you only use VBox

    To answer to your question, simply use an HBox as parent of your GridPane with alignment property set to Pos.CENTER.

    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
            BorderPane bord = new BorderPane();
            HBox hb = new HBox();
            GridPane griglia = new GridPane();
            Text server = new Text("SERVER");
            griglia.add(server, 1, 0);
            griglia.add(new Text("Numero Porta"), 0, 1);
            griglia.add(new TextField(), 1, 1);
            griglia.setVgap(5.0);
            griglia.setHgap(5.0);
            GridPane.setValignment(server, VPos.CENTER);
            GridPane.setHalignment(server, HPos.CENTER);
            hb.getChildren().add(griglia);
            hb.setStyle("-fx-border-color:red");
            hb.setAlignment(Pos.CENTER);
            bord.setTop(hb);
            primaryStage.setScene(new Scene(bord, 800, 500));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }