VBOX gets pushed off the scene when put into BorderPane

564 Views Asked by At

I have 3 HBox's set inside a vbox and it displays perfectly centered, up until i try to fit the vbox inside the borderPane. Im trying to have a menu going across the top of the scene and have the rest of my labels and text fields in the center, except my vbox gets pushed to the upper right corner when its put into the border pane. Heres my code, thank you in advance.

 //COURSE TITLE PANE
    HBox courseTitlePane = new HBox(30);
    courseTitlePane.getChildren().addAll(courseTitleLabel,courseTitleField);
    courseTitlePane.setAlignment(Pos.CENTER);
    //
    //COURSE NUMBER PANE
    HBox courseNumberPane = new HBox(30);
    courseNumberPane.getChildren().addAll(courseNumberLabel,courseNumberField);
    courseNumberPane.setAlignment(Pos.CENTER);
    //
    //COURSE CREDITS PANE
    HBox creditsPane = new HBox(30);
    creditsPane.getChildren().addAll(numOfCreditsLabel,numOfCreditsField);
    creditsPane.setAlignment(Pos.CENTER);
    //
    //COURSE DESCRIPTION PANE
    HBox descriptionPane = new HBox(30);
    descriptionPane.getChildren().addAll(courseDescriptionLabel,courseDescriptionField);
    descriptionPane.setAlignment(Pos.CENTER);
    //

    VBox pane = new VBox(30);
    pane.getChildren().addAll(courseTitlePane,courseNumberPane,creditsPane,descriptionPane);
    pane.setAlignment(Pos.CENTER);

    BorderPane root = new BorderPane();
    root.getChildren().addAll(pane);
    root.setTop(menuBar);

    Scene scene = new Scene(root,800,500);
    primaryStage.setScene(scene);
    primaryStage.show();
1

There are 1 best solutions below

0
On

@James_D comment is indeed correct. If you look at the BorderPane documentation it describes that a BorderPane "lays out children in top, left, right, bottom, and center positions."

So just like you set your menuBar to be added to the top of the BorderPane with root.setTop(menuBar);, to set the center content of the BorderPane you must use root.setCenter(pane);.

Here is a full example:

package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        HBox courseTitlePane = new HBox(30);
        Label courseTitleLabel = new Label("Course Title");
        TextField courseTitleField = new TextField();
        courseTitlePane.getChildren().addAll(courseTitleLabel,courseTitleField);
        courseTitlePane.setAlignment(Pos.CENTER);
        //
        //COURSE NUMBER PANE
        HBox courseNumberPane = new HBox(30);
        Label courseNumberLabel = new Label("Course Number");
        TextField courseNumberField = new TextField();
        courseNumberPane.getChildren().addAll(courseNumberLabel,courseNumberField);
        courseNumberPane.setAlignment(Pos.CENTER);
        //
        //COURSE CREDITS PANE
        HBox creditsPane = new HBox(30);
        Label numOfCreditsLabel = new Label("Credits");
        TextField numOfCreditsField = new TextField();
        creditsPane.getChildren().addAll(numOfCreditsLabel,numOfCreditsField);
        creditsPane.setAlignment(Pos.CENTER);
        //
        //COURSE DESCRIPTION PANE
        HBox descriptionPane = new HBox(30);
        Label courseDescriptionLabel = new Label("Course Description");
        TextField courseDescriptionField = new TextField();
        descriptionPane.getChildren().addAll(courseDescriptionLabel, courseDescriptionField);
        descriptionPane.setAlignment(Pos.CENTER);
        //

        VBox pane = new VBox(30);
        pane.getChildren().addAll(courseTitlePane,courseNumberPane,creditsPane,descriptionPane);
        pane.setAlignment(Pos.CENTER);

        final Menu menu1 = new Menu("File");
        final Menu menu2 = new Menu("Options");
        final Menu menu3 = new Menu("Help");

        BorderPane root = new BorderPane();
        MenuBar menuBar = new MenuBar();
        root.setTop(menuBar);
        menuBar.getMenus().addAll(menu1, menu2, menu3);
        root.setCenter(pane);
        primaryStage.setTitle("Some Generic Course Application");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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