You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-menubar.asciidoc 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ---
  2. title: MenuBar
  3. order: 25
  4. layout: page
  5. ---
  6. [[components.menubar]]
  7. = [classname]#MenuBar#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/interaction/menu-bar"]
  11. endif::web[]
  12. The [classname]#MenuBar# component allows creating horizontal dropdown menus,
  13. much like the main menu in desktop applications.
  14. [[figure.components.menubar]]
  15. .Menu Bar
  16. image::img/menubar-example1.png[]
  17. [[components.menubar.creation]]
  18. == Creating a Menu
  19. The actual menu bar component is first created as follows:
  20. [source, java]
  21. ----
  22. MenuBar barmenu = new MenuBar();
  23. main.addComponent(barmenu);
  24. ----
  25. You insert the top-level menu items to the [classname]#MenuBar# object with the
  26. [methodname]#addItem()# method. It takes a string label, an icon resource, and a
  27. command as its parameters. The icon and command are not required and can be
  28. [parameter]#null#. The [methodname]#addItem()# method returns a
  29. [classname]#MenuBar.MenuItem# object, which you can use to add sub-menu items.
  30. The [classname]#MenuItem# has an identical [methodname]#addItem()# method.
  31. For example (the command is explained later):
  32. [source, java]
  33. ----
  34. // A top-level menu item that opens a submenu
  35. MenuItem drinks = barmenu.addItem("Beverages", null, null);
  36. // Submenu item with a sub-submenu
  37. MenuItem hots = drinks.addItem("Hot", null, null);
  38. hots.addItem("Tea",
  39. new ThemeResource("icons/tea-16px.png"), mycommand);
  40. hots.addItem("Coffee",
  41. new ThemeResource("icons/coffee-16px.png"), mycommand);
  42. // Another submenu item with a sub-submenu
  43. MenuItem colds = drinks.addItem("Cold", null, null);
  44. colds.addItem("Milk", null, mycommand);
  45. colds.addItem("Weissbier", null, mycommand);
  46. // Another top-level item
  47. MenuItem snacks = barmenu.addItem("Snacks", null, null);
  48. snacks.addItem("Weisswurst", null, mycommand);
  49. snacks.addItem("Bratwurst", null, mycommand);
  50. snacks.addItem("Currywurst", null, mycommand);
  51. // Yet another top-level item
  52. MenuItem servs = barmenu.addItem("Services", null, null);
  53. servs.addItem("Car Service", null, mycommand);
  54. ----
  55. [[components.menubar.commands]]
  56. == Handling Menu Selection
  57. Menu selection is handled by executing a __command__ when the user selects an
  58. item from the menu. A command is a call-back class that implements the
  59. [classname]#MenuBar.Command# interface.
  60. [source, java]
  61. ----
  62. // A feedback component
  63. final Label selection = new Label("-");
  64. main.addComponent(selection);
  65. // Define a common menu command for all the menu items.
  66. MenuBar.Command mycommand = new MenuBar.Command() {
  67. public void menuSelected(MenuItem selectedItem) {
  68. selection.setValue("Ordered a " +
  69. selectedItem.getText() +
  70. " from menu.");
  71. }
  72. };
  73. ----
  74. ifdef::web[]
  75. [[components.menubar.menuitem]]
  76. == Menu Items
  77. Menu items have properties such as a caption, icon, enabled, visible, and
  78. description (tooltip). The meaning of these is the same as for components.
  79. Submenus are created by adding sub-items to an item with [methodname]#addItem()#
  80. or [methodname]#addItemBefore()#.
  81. The __command__ property is a [classname]#MenuBar.Command# that is called when
  82. the particular menu item is selected. The [methodname]#menuSelected()# callback
  83. gets the clicked menu item as its parameter.
  84. Menus can have __separators__, which are defined before or after an item with
  85. [methodname]#addSeparatorBefore()# or [methodname]#addSeparator()# on the item,
  86. respectively.
  87. [source, java]
  88. ----
  89. MenuItem drinks = barmenu.addItem("Beverages", null, null);
  90. ...
  91. // A sub-menu item after a separator
  92. drinks.addSeparator();
  93. drinks.addItem("Quit Drinking", null, null);
  94. ----
  95. Enabling __checkable__ on an menu item with [methodname]#setCheckable()# allows
  96. the user to switch between checked and unchecked state by clicking on the item.
  97. You can set the checked state with [methodname]#setChecked()#. Note that if such
  98. an item has a command, the checked state is not flipped automatically, but you
  99. need to do it explicitly.
  100. Menu items have various other properties as well, see the API documentation for
  101. more details.
  102. endif::web[]
  103. [[components.menubar.css]]
  104. == CSS Style Rules
  105. [source, css]
  106. ----
  107. .v-menubar { }
  108. .v-menubar-submenu { }
  109. .v-menubar-menuitem { }
  110. .v-menubar-menuitem-caption { }
  111. .v-menubar-menuitem-selected { }
  112. .v-menubar-submenu-indicator { }
  113. ----
  114. The menu bar has the overall style name [literal]#++.v-menubar++#. Each menu
  115. item has [literal]#++.v-menubar-menuitem++# style normally and additionally
  116. [literal]#++.v-menubar-selected++# when the item is selected, that is, when the
  117. mouse pointer hovers over it. The item caption is inside a
  118. [literal]#++v-menubar-menuitem-caption++#. In the top-level menu bar, the items
  119. are directly under the component element.
  120. Submenus are floating [literal]#++v-menubar-submenu++# elements outside the menu
  121. bar element. Therefore, you should not try to match on the component element for
  122. the submenu popups. In submenus, any further submenu levels are indicated with a
  123. [literal]#++v-menubar-submenu-indicator++#.
  124. ifdef::web[]
  125. [[components.menubar.css.menuitems]]
  126. === Styling Menu Items
  127. You can set the CSS style name for the menu items with
  128. [methodname]#setStyleName()#, just like for components. The style name will be
  129. prepended with [literal]#++v-menubar-menuitem-++#. As [classname]#MenuBar# does
  130. not indicate the previous selection in any way, you can do that by highlighting
  131. the previously selected item. However, beware that the [literal]#++selected++#
  132. style for menu items, that is, [literal]#++v-menubar-menuitem-selected++#, is
  133. reserved for mouse-hover indication.
  134. [source, java]
  135. ----
  136. MenuBar barmenu = new MenuBar();
  137. barmenu.addStyleName("mybarmenu");
  138. layout.addComponent(barmenu);
  139. // A feedback component
  140. final Label selection = new Label("-");
  141. layout.addComponent(selection);
  142. // Define a common menu command for all the menu items
  143. MenuBar.Command mycommand = new MenuBar.Command() {
  144. MenuItem previous = null;
  145. public void menuSelected(MenuItem selectedItem) {
  146. selection.setValue("Ordered a " +
  147. selectedItem.getText() +
  148. " from menu.");
  149. if (previous != null)
  150. previous.setStyleName(null);
  151. selectedItem.setStyleName("highlight");
  152. previous = selectedItem;
  153. }
  154. };
  155. // Put some items in the menu
  156. barmenu.addItem("Beverages", null, mycommand);
  157. barmenu.addItem("Snacks", null, mycommand);
  158. barmenu.addItem("Services", null, mycommand);
  159. ----
  160. You could then style the highlighting in CSS as follows:
  161. [source, css]
  162. ----
  163. .mybarmenu .v-menubar-menuitem-highlight {
  164. background: #000040; /* Dark blue */
  165. }
  166. ----
  167. endif::web[]