0

I have an application with two menu bars (application menus and administration menus) on the top row and a search box in between them. The first menu bar is left justified with the search box immediately following while the second menu is right justified. This leaves room for additional application menus without moving the administration menus.

I tried an HBox, but can't get the second menu right justfied.

I tried using an AnchorPane and anchoring the application menu to the left and the admin menu to the right. This works fine until you resize it. When the display gets too small to show both menus it starts truncating letters. I want it to wrap the second menu to the next line.

I tried using a FlowPane which works great for getting one to flow under the other when resized, but I can't get the second menu reliable right justified. I tried a trick of putting a listener on the parent width and calculating the hgap to use, but the first time this gets called, the menu bars have a size of 0 and so the hgap is too big for the actual menus. After I resize it once, that trick works beautifully.

Even better would be a menuBar that could flow automatically so that I didn't have to break it up to allow it to wrap around. But if that capability exists, I've been unable to find it.

1
  • you can define controls in scene builder and in layout section you can set constraints of them.after that if you maximize your window from your size no issue occurred and for small sizing you need to use widthProperty() for pane and controls. Commented Nov 7, 2013 at 11:28

1 Answer 1

1

AFAIK MenuBar does not support wrapping its Menus.
There can be different approaches to achieve the layout you want. One of them at the below.
To align the second admin flowPane to the right, use HBox.setHgrow for flowPane. To align menu bars in flowPane, use flow.setAlignment(Pos.TOP_RIGHT):

@Override
public void start(Stage primaryStage) {

    final Menu menu01 = new Menu("App Menu 1");
    final Menu menu02 = new Menu("App Menu 2");
    final Menu menu1 = new Menu("Admin Menu 1");
    final Menu menu2 = new Menu("Admin Menu 2");
    final Menu menu3 = new Menu("Admin Menu 3");

    MenuBar menuBar0 = new MenuBar();
    menuBar0.getMenus().addAll(menu01, menu02);
    menuBar0.setMinWidth(220); // do not shrink

    MenuBar menuBar1 = new MenuBar();
    menuBar1.getMenus().addAll(menu1);

    MenuBar menuBar2 = new MenuBar();
    menuBar2.getMenus().addAll(menu2);

    MenuBar menuBar3 = new MenuBar();
    menuBar3.getMenus().addAll(menu3);

    FlowPane flow = new FlowPane(Orientation.HORIZONTAL);
    // flow.setStyle("-fx-background-color: gray; -fx-border-color: red");  // visual debug
    flow.setAlignment(Pos.TOP_RIGHT);
    flow.setHgap(0);
    flow.getChildren().addAll(menuBar1, menuBar2, menuBar3);

    TextField searchField = new TextField();
    searchField.setPromptText("Search here..");
    // make it unresizable
    searchField.setMinWidth(200);
    searchField.setMaxWidth(200);

    HBox mainBox = new HBox(5);
    mainBox.setAlignment(Pos.CENTER_LEFT);
    HBox.setHgrow(flow, Priority.ALWAYS);
    mainBox.getChildren().addAll(menuBar0, searchField, flow);
    mainBox.setStyle("-fx-background-color: lightgray;");

    VBox vBox = new VBox(0);
    vBox.getChildren().addAll(mainBox, new Button("Demo"));

    Scene scene = new Scene(vBox);
    primaryStage.setScene(scene);
    primaryStage.show();
}
3
  • That would be great except that menuBar does not support an alignment attribute. I checked the the fxml, the code and the docs.
    – kithril
    Commented Nov 6, 2013 at 22:00
  • @kithril, it was flowPane not menuBar actually. Edited the answer with sample code to demo the layout that I have understood from your description.
    – Uluk Biy
    Commented Nov 7, 2013 at 1:40
  • Thanks. Now I see how it's working. I didn't consider the HBox.setHgrow() and HBox alignment together would do that trick.
    – kithril
    Commented Nov 8, 2013 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.