3

I can't for the life of me figure out why I can't center a VBox within one of the sections within a border pane.

@Override
public void start(Stage primaryStage) throws Exception{
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 960, 600);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Test application");
    primaryStage.setFullScreen(true);
    primaryStage.setFullScreenExitHint("Press escape to exit fullscreen");
    primaryStage.show();
    javafx.scene.image.Image icon = new Image("sample/Riverviewcrest.png");
    primaryStage.getIcons().add(icon);

    Pane paneleft = new Pane();
    Pane paneright = new Pane();
    Pane panecenter = new Pane();
    Pane panetop = new Pane();
    Pane panebottom = new Pane();

    paneleft.setPrefWidth(100);
    paneright.setPrefWidth(100);
    panetop.setPrefHeight(100);
    panebottom.setPrefHeight(100);

    panecenter.setStyle("-fx-background-color: #0053A8");

    root.setLeft(paneleft);
    root.setRight(paneright);
    root.setCenter(panecenter);
    root.setTop(panetop);
    root.setBottom(panebottom);


    Text test = new Text("Hello");
    Text test2 = new Text("Hello");
    Text test3 = new Text("Hello");
    Text test4 = new Text("Hello");
    VBox box = new VBox();
    box.getChildren().addAll(test, test2, test3, test4);
    panecenter.getChildren().add(box);
    root.setAlignment(box, Pos.CENTER);
}

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

The aim is to have the text centered within the center part of the border pane. Thanks for any and all help!

TT.
  • 15,774
  • 6
  • 47
  • 88

2 Answers2

3

No need to have the panecenter at all. Simply set box to the center property of your BorderPane and center it:

box.setAlignment(Pos.CENTER);
root.setCenter(box);

You don't need paneleft, paneright, panetop or panebottom either.

Edvin Syse
  • 7,267
  • 18
  • 24
  • But then how do I initialize those parts of the border pane if I want them there? Or should I use a box for all of them. – Archie Croston Feb 28 '16 at 10:06
  • What ever you want to set into either of the BorderPane positions, just do borderpane.setX(yourNode). where X is top, bottom, left or right. The extra Pane just adds complexity to the scene graph with no added benefit. – Edvin Syse Feb 28 '16 at 11:54
0

paneCenter is just a pane like everything else. So paneCenter gets centered, not the VBox.

You can use a StackPane and adapt the VBox's alignment like this:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 960, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test application");
        primaryStage.show();

        Pane paneleft = new Pane();
        Pane paneright = new Pane();
        StackPane panecenter = new StackPane();
        Pane panetop = new Pane();
        Pane panebottom = new Pane();

        paneleft.setPrefWidth(100);
        paneright.setPrefWidth(100);
        panetop.setPrefHeight(100);
        panebottom.setPrefHeight(100);

        panecenter.setStyle("-fx-background-color: #0053A8");

        root.setLeft(paneleft);
        root.setRight(paneright);
        root.setCenter(panecenter);
        root.setTop(panetop);
        root.setBottom(panebottom);


        Text test = new Text("Hello");
        Text test2 = new Text("Hello");
        Text test3 = new Text("Hello");
        Text test4 = new Text("Hello");
        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.getChildren().addAll(test, test2, test3, test4);
        panecenter.getChildren().add(box);
    }

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

Or you could drop the panecenter and use the VBox instead.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 960, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test application");
        primaryStage.show();

        Pane paneleft = new Pane();
        Pane paneright = new Pane();
        StackPane panecenter = new StackPane();
        Pane panetop = new Pane();
        Pane panebottom = new Pane();

        paneleft.setPrefWidth(100);
        paneright.setPrefWidth(100);
        panetop.setPrefHeight(100);
        panebottom.setPrefHeight(100);

        panecenter.setStyle("-fx-background-color: #0053A8");

        root.setLeft(paneleft);
        root.setRight(paneright);
        root.setTop(panetop);
        root.setBottom(panebottom);


        Text test = new Text("Hello");
        Text test2 = new Text("Hello");
        Text test3 = new Text("Hello");
        Text test4 = new Text("Hello");
        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.getChildren().addAll(test, test2, test3, test4);
        root.setCenter(box);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Roland
  • 18,114
  • 12
  • 62
  • 93