Skip to content

PopupMenu and MenuBar

intrigus edited this page Aug 23, 2015 · 29 revisions

VisUI provides PopupMenu (source) and MenuBar (source) widgets. Since 0.7.0 submenus are supported. Menu automatically hides itself when menu item was clicked.

PopupMenu screenshot

PopupMenu

PopupMenu is a standard menu that can be displayed anywhere on stage. Any widget can be added to it however it provides two methods for adding standard menu items and menu separators:

menu.addItem (MenuItem item)
menu.addSeparator ()

Displaying menu

To display or remove menu call:

menu.displayMenu (Stage stage, float x, float y)
menu.remove ()

displayMenu() makes sure that menu stays inside stage, alternative way is to add menu like any other scene2d.ui widget.

Removed menu can be reused later. Method is called remove and not hide because the menu is actually completely removed from stage and not only hidden.

MenuItem

MenuItem is a component that is displayed inside PopupMenu. It provides various constructors, that allows to construct MenuItem with text only, or with text and an icon. There are also constructors that takes ChangeListener along with text and icon. Best icon size is 22x22px.

Listener is called when button has been clicked.

Shorcuts

Additionally you can set shortcut that will be displayed next to menu text, shortcut text is gray and is right aligned (see screenshot above). setShortcut() method can take int value from LibGDX Keys class or custom text. This methods returns current MenuItem for the purpose of chaining method together. Please note that shortcuts are only decoration, listener won't be called if user will press key that you set for shortcut.

Submenus

To create a submenu you have to create a standard PopupMenu and then assign it to a menu item in parent menu:

menuItem.setSubMenu(somePopupMenu);

After that menu item will automatically display submenu when user moves mouse pointer over that item. You can have as many nested submenus as you want.

Menu and MenuBar

MenuBar is a bar with menus, usually displayed on top of the stage.

Displaying

Recommended way to add menu is:

root.add(menuBar.getTable()).fillX().expandX().row();
//or (if you have more than one column in root table)
root.add(menuBar.getTable()).fillX().expandX().colspan(x).row();

Menus

To add menu to MenuBar call:

menuBar.addMenu(Menu menu)

The only difference between Menu and PopupMenu is that Menu constructor takes title, that will be used on MenuBar. You can add MenuItems and separators same way you added them to PopupMenu.