Make vertical Menu Bar in JavaFX


Since my interaction with computer, I have seen only menu bar in horizontal direction only. The menuitem of such menubar will be popping downwards. In JavaFX it is easy to create such a menu with a horizontal menubar.

Is it possible to create a vertical menubar in JavaFX ? Also I want the menuitems to be popped out either to left or right, not downwards.

Can I implement such a menu of my desire ? Someone please help.

3/26/2014 10:51:55 AM

Accepted Answer

You can leverage the MenuButton for that:

public void start(Stage primaryStage) {
    MenuButton m = new MenuButton("Eats");
    m.getItems().addAll(new MenuItem("Burger"), new MenuItem("Hot Dog"));

    MenuButton m2 = new MenuButton("Drinks");
    m2.getItems().addAll(new MenuItem("Juice"), new MenuItem("Milk"));

    VBox root = new VBox();
    root.getChildren().addAll(m, m2);

    Scene scene = new Scene(root, 300, 250);

where the style.css is

.menu-button {
    -fx-skin: "";
    -fx-background-color: red, green, green, lightgreen;
    -fx-background-insets: 0 0 -1 0, 0, 1, 2;
    -fx-background-radius: 0;
    -fx-padding: 0.0em; /* 0 */
    -fx-text-fill: -fx-text-base-color;

/* TODO workaround for RT-19062 */
.menu-button .label { -fx-text-fill: -fx-text-base-color; }

.menu-button:focused {
    -fx-color: beige;
    -fx-background-color: -fx-focus-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
    -fx-background-insets: -1.4, 0, 1, 2;
    -fx-background-radius: 0;

.menu-button:hover {
    -fx-color: darkgreen;

.menu-button:armed {
    -fx-color: greenyellow;

These selectors are partially taken and overriden from caspian.css. Change the color preferences as your needs and you can also remove the arrow of the buttons through css.

The drawback of this approach is the difficulty of making nested menu items.

3/26/2014 1:27:52 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow