Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

components-menubar.asciidoc 6.8KB

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