how to load MenuBar with FXMLloader

1

Hi, I'm trying to load a menubar by calling the file, visual.fxml, as I do to load the menubar, in the same scene or stage as the document, which I can correct, I'm new to this, and I need some guidance. Here I leave my code. thanks:

public void start(Stage primaryStage) throws IOException{

    primaryStage.setTitle("THANOS V1.0 ");
    AnchorPane root = new AnchorPane();
    root = FXMLLoader.load(getClass().getResource("Visualizar.fxml"));
    Scene scene = new Scene(root,  Color.WHITE);
    primaryStage.setScene(scene);
    primaryStage.show();

    MenuBar menuBar = new MenuBar();//Barramenu
    BorderPane raiz = new BorderPane();
    raiz.setTop(menuBar);
    Menu Agregar = new Menu("Archivo"); //creando Menu archivo
    MenuItem PrimerItem= new MenuItem("Agregar Datos"); //creando menu item
    Agregar.getItems().addAll(PrimerItem);
    menuBar.getMenus().addAll(Agregar);



}
    
asked by Rafael 16.10.2018 в 18:40
source

1 answer

1
  

How do I load the menubar

As your code is displayed, the main container is the AnchorPane . You will have to create MenuBar and add it to root before adding this main container to scene .

Also to add that menubar to the root just do this ..

root.getChildren().add(menuBar);

I leave you the method start()

@Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("THANOS V1.0");
        //AnchorPane root = new AnchorPane();
        AnchorPane root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));      

        MenuBar menuBar = new MenuBar();//Barramenu

        Menu agregar = new Menu("Archivo");//Creando menu archivo
        MenuItem primerItem = new MenuItem("Agregar Datos"); //creando menu item
        agregar.getItems().add(primerItem);
        menuBar.getMenus().add(agregar);

        root.getChildren().add(menuBar);

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

Result

For more information about how the AnchorPane layout works and how to place the elements inside it Documentation

    
answered by 17.10.2018 в 11:56