Phylonias
Phylonias

Reputation: 21

JavaFX - BorderPane is not centered in scene/stage

I am currently coding a board game and I am using java and for GUI purposes javaFX. The game requires for a map (the game board) to be in the middle of the screen and various options and additional information to be around it. My idea was to have a BorderPane where the center node is the game board and top, bottom etc are the additional options.

My problem is that upon starting the App the BorderPane is not centered in the stage but is slightly extended to the right and botton, where it is not visible. Thus my bottom Node can't be seen. Strangely if I minimize the window and maximize it again everything is where it should be and perfectly inside the bounds of the stage.

The application before minimizing and maximizing again

And afterwards (The way it should look like from the beginning)

My center Node is a normal Pane. Also I do stage.setMaximize(true). So the window is already maximized upon starting the application and it should not make a difference to minimze and maximize again.

The code for this scene essentially boils down to this:

@Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        Pane pane = createGameBoard();
        pane.setId("mapPane");

        Button button = new Button("Save");

        VBox box = new VBox(0, button);
        box.setAlignment(Pos.CENTER);

        Label label = new Label("Bottom");
        label.setPrefHeight(20);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(top);
        borderPane.setBottom(bottom);
        borderPane.setCenter(center);
        borderPane.setPadding(new Insets(10));

        Scene scene = new Scene(borderPane);
        
        scene.getStylesheets().add(Objects.requireNonNull(MainGUI.class.getResource("/game.css")).toString());

        stage.setScene(scene);
    }

The game.css stylesheet only sets a background colour at the moment. I am using: Java 17, JavaFX 17.

If you need any further information I am happy to provide it :)

Thanks!

Edit: To reproduce my problem run this code. The issue should appear once you click on the button "Next Screen".

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class App extends Application {

    private Stage stage;

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

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle("App");
        stage.setMinHeight(600);
        stage.setMinWidth(800);
        stage.setWidth(java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth());
        stage.setHeight(java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        stage.setMaximized(true);
        stage.setOnCloseRequest(e -> System.exit(0));

        Button button = new Button("Next Screen");
        button.setOnAction(e -> {gameScreen();});

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(button);
        
        Scene scene = new Scene(borderPane);

        stage.setScene(scene);

        stage.show();
    }

    private void gameScreen() {
        Circle circle = new Circle(0, 0, 4);
        Pane pane = new Pane(circle);
        pane.setStyle("-fx-background-color: #00c3ff");

        Button button = new Button("Top");
        VBox vBox = new VBox(0, button);
        vBox.setAlignment(Pos.CENTER);

        Label label = new Label("Bottom");

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(vBox);
        borderPane.setCenter(pane);
        borderPane.setBottom(label);
        borderPane.setPadding(new Insets(10));

        Scene scene = new Scene(borderPane);

        stage.setScene(scene);
    }

}

Upvotes: 1

Views: 450

Answers (1)

Phylonias
Phylonias

Reputation: 21

My problem was resolved by changing the root of the scene I was using and not create a new scene everytime I switch the layout (e.g. from main menu to the game screen).

Upvotes: 1

Related Questions