Skip to content

PopupMenu and MenuBar

kotcrab edited this page Dec 13, 2014 · 29 revisions

VisUI provides PopupMenu and MenuBar widgets. Currently menus cannot have submenus.

PopupMenu screenshot

PopupMenu

PopupMenu is standard 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.

Auto remove

Menus can also be removed automatically if user clicked outside it, that feature is disabled by default. To enable it call:

menu.setAutoRemove (true) 

Alternatively you can use PopupMenu (boolean autoRemove) constructor.

Even if auto remove is enabled menu won't be removed if user clicked inside menu, that means clicking on menu items doesn't remove menu automatically. If you want to remove menu on menu item click you have to do that from menu item listener.

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 a icon. They are also constructors that takes ChangeListener along with text and icon.

Listener is called when button has been clicked.

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.

Menu and MenuBar