3

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??

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Mubasher
  • 943
  • 1
  • 13
  • 36
  • Do you want different `MenuBar` for different `Scene` ? – ItachiUchiha Aug 28 '14 at 08:57
  • No I Said One MenuBar for All Scenes. All Scenes should use one menu bar defined by "menus.fxml" – Mubasher Aug 28 '14 at 09:16
  • 1
    Why do you need multiple scenes to have same `MenuBar` ? Even if that the case, why do you need multiple `Scene` ? May be you should explain the use of multiple scenes in your application and how are you swapping between the scenes. The reason I am asking these questions is because I feel there is no need for multiple scenes in your application – ItachiUchiha Aug 28 '14 at 09:17
  • First Scene Has split Pane (no tool bar), 2nd Scene is a Web View with its tool bar. Now both should has same menu bar at top which shows Alerts, Options, Help. These Menus Required By both Scenes. I switch by calling parrentStage.setScene(myNewScene); – Mubasher Aug 28 '14 at 09:28
  • can you suggest me a way by which i don't have to switch scenes. I mean can I achieve switching layouts in one scene, and switching is CPU friendly, as both layout loaded with heavy components. – Mubasher Aug 28 '14 at 09:40

1 Answers1

10

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

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • seems to me like a solution. is maximizing will automatically expand the all inner components? – Mubasher Aug 28 '14 at 13:00
  • 1
    Yes it will. All the Layouts in Javafx are automatically resized depending on the container size ! – ItachiUchiha Aug 28 '14 at 13:02
  • It then becomes hard to do a center switch in a different controller, as one can't use `getScene().setParent(anotherView);` anymore. You will have to keep a reference to a shared BorderPane or deal with locating it in the parent scene.. Could you please update your answer with multiple controllers making a center view switch by button click? – Vadim Kirilchuk Jun 18 '21 at 14:39