Like Acheive One Menubar for more than one scene in javaFx

I am basically new to Java FX 2.

Scenario:

I have 3 scenes, and I need a way to add a menu bar so that I don't want to explicitly remove the menu bar from the previous scene and add it to a new one. Like some things, the Parent Scene scenario or some menu bar is tied to Stage. I mean, the menu bar is added only once and is always present, regardless of which scene is ahead or not.

If 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 the menuBar is added to the scene. if I change the scene and bring another scene ahead ... What will I do. I think I am removing the menuBar from this scene and adding to another or just adding to a new one. so every time I have to do it when I change. Is there any way to avoid this?

+1
source share
1 answer

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

 scene.setRoot(borderPane); 

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

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

If you need to switch to WebView , just replace it with SplitPane :

 borderPane.setCenter(webView); 

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

+6
source

Source: https://habr.com/ru/post/1395946/


All Articles