How to Acheive One Menubar for more then one scene

2019-02-27 09:54发布

I am basically new to Java FX 2.

Scenario:

I have 3 Scenes and I want a way to add menu-bar such that I don't i don't want to explicitly remove the menu bar from previous scene and add it to new one. Like Some thing a Parent Scene or some way menu-bar is attached to Stage. I mean menu-bar is added just one time and always be present whatever scene is in front or not.

If This is Possible How Can I do this.

Here is the Default Example Provided by Oracle Docs of JavaFX http://docs.oracle.com/javafx/2/ui_controls/MenuSample.java.html

public class Main extends Application {
 final ImageView pic = new ImageView();
 final Label name = new Label();
 final Label binName = new Label();
 final Label description = new Label();

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

 @Override
 public void start(Stage stage) {

   stage.setTitle("Menu Sample");
   Scene scene = new Scene(new VBox(), 400, 350);
   scene.setFill(Color.OLDLACE);

   MenuBar menuBar = new MenuBar();

   // --- Graphical elements
    final VBox vbox = new VBox();
    vbox.setAlignment(Pos.CENTER);
    vbox.setSpacing(10);        
    vbox.setPadding(new Insets(0, 10, 0, 10));
    makeContentsForVBox();// in this vBox area will be fill with name pic desrciption
    vbox.getChildren().addAll(name, binName, pic, description); // name is lable


   // --- Menu File
    Menu menuFile = new Menu("File");
    MenuItem add = new MenuItem("Shuffle",
        new ImageView(new Image(getClass().getResourceAsStream("new.png"))));
    add.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            shuffle();
            vbox.setVisible(true);
        }
    });

    MenuItem clear = new MenuItem("Clear");
    clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
    clear.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            vbox.setVisible(false);
        }
    });

    MenuItem exit = new MenuItem("Exit");
    exit.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            System.exit(0);
        }
    });

    menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);
    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);

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

So Here menuBar is added to a scene. if i swap the scene and bring an other scene in front ... What will i do. i think I remove menuBar from this scene and add to other or simply add to new one. so every time i have to do this when i change. Is there any way to avoid this??

1条回答
\"骚年 ilove
2楼-- · 2019-02-27 10:38

The approach I would prefer is to use a Scene with BorderPane as its root

scene.setRoot(borderPane);

You can add the MenuBar to the top of the BorderPane and at its Center you can place SplitPane

BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(splitPane);

Whenever you need to switch to WebView just replace it with SplitPane :

borderPane.setCenter(webView);

Following this approach, your MenuBar will always remain on TOP and you can switch between SplitPane and WebView

查看更多
登录 后发表回答