Question

I would like to add a javafx.scene.control.MenuBar into an AnchorPane. The number of menu entries is flexible. The user could add one or more entries into the MenuBar. If I only use a few menu entries in the MenuBar, then there is enough horizontal place.

MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
Menu menuEdit = new Menu("Edit");
Menu menuView = new Menu("View");
menuBar.getMenus().addAll(menuFile, menuEdit, menuView);

But in case of more than a few entries and some long items, there is not enough place. This is not the normal case, but some users would like to add more menu items. The maximum number of menu items will be limited to ten entries.

MenuBar menuBar = new MenuBar();
Menu menu1 = new Menu("Long first menu");
Menu menu2 = new Menu("My second long entry");
Menu menu3 = new Menu("another long entry");
Menu menu4 = new Menu("Long menu 4");
Menu menu5 = new Menu("My fifth long entry");
Menu menu6 = new Menu("another long long entry");

menuBar.getMenus().addAll(menu1, menu2, menu3, menu4, menu5, menu6);

My Dialog must be not resizable. and all menus have to be in one line in the MenuBar. This is a GUI design decision.

I would like to have a solution with a scrollable MenuBar. If this solution is absolutely not possible the next solution could be to reduce the number of the shown characters of all menus. e.g.: |Long fir...|My second...|another...| ... but I prefer the first solution.

Was it helpful?

Solution

reduce the number of the shown characters of all menus. e.g.: |Long fir...|My second...|another...| ...

This is the default behaviour for a MenuBar placed inside a resizable pane.

Here is some sample code using your long menu examples.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class ElidedMenu extends Application {
    @Override public void start(Stage stage) {
        MenuBar menuBar = new MenuBar();
        Menu menu1 = new Menu("Long first menu");
        Menu menu2 = new Menu("My second long entry");
        Menu menu3 = new Menu("another long entry");
        Menu menu4 = new Menu("Long menu 4");
        Menu menu5 = new Menu("My fifth long entry");
        Menu menu6 = new Menu("another long long entry");

        menuBar.getMenus().addAll(menu1, menu2, menu3, menu4, menu5, menu6);      

        StackPane stack = new StackPane();
        stack.getChildren().add(new Label("Hello World!"));

        VBox layout = new VBox();
        layout.getChildren().addAll(menuBar, stack);
        VBox.setVgrow(stack, Priority.ALWAYS);

        stage.setScene(new Scene(layout, 500, 250));
        stage.setResizable(false);
        stage.show();
    }

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

The output of the sample code is:

elidedmenu


a solution with a scrollable MenuBar

Wrap the MenuBar in a ScrollPane to make it scrollable.

Here is some sample code using your long menu examples.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class ScrollingMenu extends Application {
    @Override public void start(Stage stage) {
        MenuBar menuBar = new MenuBar();
        Menu menu1 = new Menu("Long first menu");
        Menu menu2 = new Menu("My second long entry");
        Menu menu3 = new Menu("another long entry");
        Menu menu4 = new Menu("Long menu 4");
        Menu menu5 = new Menu("My fifth long entry");
        Menu menu6 = new Menu("another long long entry");

        menuBar.getMenus().addAll(menu1, menu2, menu3, menu4, menu5, menu6);      
        ScrollPane scrollingMenu = new ScrollPane();
        scrollingMenu.setContent(menuBar);

        StackPane stack = new StackPane();
        stack.getChildren().add(new Label("Hello World!"));

        VBox layout = new VBox();
        layout.getChildren().addAll(scrollingMenu, stack);
        VBox.setVgrow(stack, Priority.ALWAYS);

        stage.setScene(new Scene(layout, 500, 250));
        stage.setResizable(false);
        stage.show();

        scrollingMenu.setPrefSize(stage.getScene().getWidth(), menuBar.getHeight());
    }

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

The output of the sample code is:

scrollingmenu

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top