2

How do I add a MenuBar to a stage? I have tried including it in my GridPane (which I have added all my other elements to) but it never looks as if it is attached to the top of the window.

Example of what it looks like.

Is there anyway to solve this?

I used this code to place it in the layout.

 layout.add(menuBar,0,1,10,1);

Where layout is the GridPane, and menuBar is the MenuBar with the menu things added.

fabian
  • 80,457
  • 12
  • 86
  • 114
MrDaveForDays
  • 121
  • 1
  • 7

1 Answers1

2

If you are doing this in code, try using a better node as the root. When I have a MenuBar, I love to use VBox. You can add your GridPane after the MenuBar.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication249 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        Menu miFile = new Menu("File");
        MenuBar menuBar = new MenuBar();
        menuBar.getMenus().add(miFile);

        VBox root = new VBox(menuBar);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

enter image description here

SedJ601
  • 12,173
  • 3
  • 41
  • 59