Menus
It's time for more concrete less abstract stuff. Let's talk about menus. Menus are a way to group functionality in a very small area of the page. The first step to create a menu, is to instantiate a new MenuBar
:
MenuBar menuBar = new MenuBar();
Now, we can add a hierarchy of instances of MenuItem
. To add items directly to the menu bar, you can do this:
MenuItem submenu1 = menuBar.addItem("Submenu 1", null); MenuItem submenu2 = menuBar.addItem("Submenu 2", null);
This will show the following menu:
We can add child elements to the submenus:
submenu1.addItem("Option 1", null); submenu1.addItem("Option 2", null); submenu2.addItem("Option 3", null); submenu2.addItem("Option 4", null);
Now our submenus have options:
You can continue and add child items to the options, and child items to the child items, and so forth.
Adding behavior when an item is selected is kind of similar to adding a click listener to a button. We need to add a Command
instance (instead of a ClickListener
) and we can do it directly...