0

I'm very new to JavaFX. I am using a grid pane to keep my items centered on the page regardless of how the window is resized. I want to add a menu that runs along the top. I quickly found out grid.setTop(menuBar) is not a member function of grid pane. Is there a way to this?

Can I create two different types of panes in one scene? IE, a GridPane to center items and a BorderPane to get the menu up top? Or should I use CSS styling to get the menubar at the top?

Here is some code I'm using:

public void start(Stage primaryStage) {
    try {
    primaryStage.setTitle("Bapu Inventory");
    BorderPane root = new BorderPane();
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));
    Text scenetitle = new Text("Welcome");
    grid.add(scenetitle, 0, 0, 1, 1);


    MenuBar menuBar = new MenuBar();
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
//This is the line I can't figure out. How do I get this to position at top left?
    grid.setTop(menuBar);

Any help would be greatly appreciated here. I looked through the documentation Oracle provides but didn't find this feature listed anywhere.

  • My suggestion: Use `BorderPane`, add your Menu to its `top`. Then add your `GridPane` to the BorderPane's center. – ihavenoidea Oct 23 '18 at 03:56
  • After creating the BorderPane is there anything special I have to do? When I try that it seems like the GridPane overwrites whatever I've had in the BorderPane. – Nick Tydryszewski Oct 23 '18 at 04:00
  • If you only add a single element to the `GridPane` it would be better to use a simpler layout. `VBox` would allow you to add both as children and could be set up to horizontally align the `Text`. `VBox.margin` could be used to replace the padding... – fabian Oct 23 '18 at 09:45

1 Answers1

1

Check if this is what you want:

public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();

            //Create your menu
            final Menu menu1 = new Menu("File");
            final Menu menu2 = new Menu("Options");
            final Menu menu3 = new Menu("Help");
            MenuBar menuBar = new MenuBar();
            menuBar.getMenus().addAll(menu1, menu2, menu3);

            //Your GridPane
            GridPane grid = new GridPane();
            grid.setAlignment(Pos.CENTER);
            grid.setHgap(10);
            grid.setVgap(10);
            grid.setPadding(new Insets(25, 25, 25, 25));
            Text scenetitle = new Text("Welcome");
            grid.add(scenetitle, 0, 0, 1, 1);

            //Add them  to root (BorderPane)
            root.setTop(menuBar);
            root.setCenter(grid);


            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
}
ihavenoidea
  • 629
  • 1
  • 7
  • 26