dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Sharing MenuBar with JavaFX

  1. #1
    Join Date
    Feb 2020
    Posts
    2

    Sharing MenuBar with JavaFX

    I'm in the middle of a project for school and ran into a problem with using a menubar across multiple scenes and classes. I am able to pass the menubar to the other class which generates a new scene and can switch back to the main scene from the menu, however, once I do the menubar is missing from the main scene. My application looks something like this:

    Code:
    From Main.java
    getMainScene() {    
        //build the scene items including menuBar
        ...
        Scene mainScene = new Scene(rootNode)
        mainSceneMenuItem.listener {
            primaryStage.setScene(mainScene)
        newSceneMenuItem.listener {
            NewPage newPage = NewPage(menuBar)
            Scene newScene = newPage.getNewScene()
            primaryStage.setScene(newScene)
        }
    }
    
    From NewScene.java
    getNewScene() {
        //build the scene items including the menuBar passed when the NewScene object was initialized
        ...
        return new Scene(rootNode)
    }
    Although my menubar, menu, and menu items are all built before my main scene is built and the menubar is passed to the other scene once returning to the mainscene from the newscene the menubar is gone, which is a bit puzzling.
    From what I have read online it seems like the common way to share the menuBar across multiple windows is to actually never change the scene but instead use a borderpane with the menubar in the top and a splitpane in the center for everything else. Is this because a menuBar cannot be properly shared to other scenes.

  2. #2
    Join Date
    Feb 2020
    Posts
    2

    Re: Sharing MenuBar with JavaFX

    With some help from a mentor at school I was able to resolve the issue and keep the menu.
    In the original code I was creating the main scene right before loading it on the stage and then using this previously created scene to load the main page anytime someone requested it. I don't know why this caused the menu to be missing but changing the code to completely rebuild the main scene every time it is requested solved the issue.
    My main scene action now looks like this instead of just the commented line:
    homeItem.setOnAction(e -> {
    try {
    Scene scene = getHomeScene(stage);
    stage.setScene(scene);
    //stage.setScene(mainScene);
    } catch (IOException ex) {
    Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
    }
    });

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)