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.

layout-tabsheet.asciidoc 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ---
  2. title: TabSheet
  3. order: 9
  4. layout: page
  5. ---
  6. [[layout.tabsheet]]
  7. = [classname]#TabSheet#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/structure/tab-sheet"]
  11. endif::web[]
  12. The [classname]#TabSheet# is a multicomponent container that allows switching
  13. between the components with "tabs". The tabs are organized as a tab bar at the
  14. top of the tab sheet. Clicking on a tab opens its contained component in the
  15. main display area of the layout. If there are more tabs than fit in the tab bar,
  16. navigation buttons will appear.
  17. [[figure.tabsheet.example1]]
  18. .A Simple TabSheet Layout
  19. image::img/tabsheet-example1.png[]
  20. [[layout.tabsheet.adding]]
  21. == Adding Tabs
  22. You add new tabs to a tab sheet with the [methodname]#addTab()# method. The
  23. simple version of the method takes as its parameter the root component of the
  24. tab. You can use the root component to retrieve its corresponding
  25. [classname]#Tab# object. Typically, you put a layout component as the root
  26. component.
  27. You can also give the caption and the icon as parameters for the
  28. [methodname]#addTab()# method. The following example demonstrates the creation
  29. of a simple tab sheet, where each tab shows a different [classname]#Label#
  30. component. The tabs have an icon, which are (in this example) loaded as Java
  31. class loader resources from the application.
  32. [source, java]
  33. ----
  34. TabSheet tabsheet = new TabSheet();
  35. layout.addComponent(tabsheet);
  36. // Create the first tab
  37. VerticalLayout tab1 = new VerticalLayout();
  38. tab1.addComponent(new Embedded(null,
  39. new ThemeResource("img/planets/Mercury.jpg")));
  40. tabsheet.addTab(tab1, "Mercury",
  41. new ThemeResource("img/planets/Mercury_symbol.png"));
  42. // This tab gets its caption from the component caption
  43. VerticalLayout tab2 = new VerticalLayout();
  44. tab2.addComponent(new Embedded(null,
  45. new ThemeResource("img/planets/Venus.jpg")));
  46. tab2.setCaption("Venus");
  47. tabsheet.addTab(tab2).setIcon(
  48. new ThemeResource("img/planets/Venus_symbol.png"));
  49. ...
  50. ----
  51. [[layout.tabsheet.tab]]
  52. == Tab Objects
  53. Each tab in a tab sheet is represented as a [classname]#Tab# object, which
  54. manages the tab caption, icon, and attributes such as hidden and visible. You
  55. can set the caption with [methodname]#setCaption()# and the icon with
  56. [methodname]#setIcon()#. If the component added with [methodname]#addTab()# has
  57. a caption or icon, it is used as the default for the [classname]#Tab# object.
  58. However, changing the attributes of the root component later does not affect the
  59. tab, but you must make the setting through the [classname]#Tab# object. The
  60. [methodname]#addTab()# returns the new [classname]#Tab# object, so you can
  61. easily set an attribute using the reference.
  62. [source, java]
  63. ----
  64. // Set an attribute using the returned reference
  65. tabsheet.addTab(myTab).setCaption("My Tab");
  66. ----
  67. [[layout.tabsheet.tab.disabling]]
  68. === Disabling and Hiding Tabs
  69. A tab can be disabled by setting [methodname]#setEnabled(false)# for the
  70. [classname]#Tab# object, thereby disallowing selecting it.
  71. A tab can be made invisible by setting [methodname]#setVisible(false)# for the
  72. [classname]#Tab# object. The [methodname]#hideTabs()# method allows hiding the
  73. tab bar entirely. This can be useful in tabbed document interfaces (TDI) when
  74. there is only one tab.
  75. ifdef::web[]
  76. [[figure.tabsheet.example2]]
  77. .A TabSheet with Hidden and Disabled Tabs
  78. image::img/tabsheet-example2.png[]
  79. endif::web[]
  80. [[layout.tabsheet.events]]
  81. == Tab Change Events
  82. Clicking on a tab selects it. This fires a
  83. [classname]#TabSheet.SelectedTabChangeEvent#, which you can handle by
  84. implementing the [classname]#TabSheet.SelectedTabChangeListener# interface. You
  85. can access the tabsheet of the event with [methodname]#getTabSheet()#, and find
  86. the new selected tab with [methodname]#getSelectedTab()#.
  87. You can programmatically select a tab with [methodname]#setSelectedTab()#, which
  88. also fires the [classname]#SelectedTabChangeEvent# (beware of recursive events).
  89. Reselecting the currently selected tab does not fire the event.
  90. Notice that when the first tab is added, it is selected and the change event is
  91. fired, so if you want to catch that, you need to add your listener before adding
  92. any tabs.
  93. ifdef::web[]
  94. [[layout.tabsheet.events.dynamic]]
  95. === Creating Tab Content Dynamically
  96. In the following example, we create the tabs as empty content layouts, and add
  97. the tab content dynamically when a tab is selected:
  98. [source, java]
  99. ----
  100. TabSheet tabsheet = new TabSheet();
  101. // Create tab content dynamically when tab is selected
  102. tabsheet.addSelectedTabChangeListener(
  103. new TabSheet.SelectedTabChangeListener() {
  104. public void selectedTabChange(SelectedTabChangeEvent event) {
  105. // Find the tabsheet
  106. TabSheet tabsheet = event.getTabSheet();
  107. // Find the tab (here we know it's a layout)
  108. Layout tab = (Layout) tabsheet.getSelectedTab();
  109. // Get the tab caption from the tab object
  110. String caption = tabsheet.getTab(tab).getCaption();
  111. // Fill the tab content
  112. tab.removeAllComponents();
  113. tab.addComponent(new Image(null,
  114. new ThemeResource("img/planets/"+caption+".jpg")));
  115. }
  116. });
  117. // Have some tabs
  118. String[] tabs = {"Mercury", "Venus", "Earth", "Mars"};
  119. for (String caption: tabs)
  120. tabsheet.addTab(new VerticalLayout(), caption,
  121. new ThemeResource("img/planets/"+caption+"_symbol.png"));
  122. ----
  123. endif::web[]
  124. [[layout.tabsheet.closing]]
  125. == Enabling and Handling Closing Tabs
  126. You can enable a close button for individual tabs with the
  127. [literal]#++closable++# property in the [classname]#TabSheet.Tab# objects.
  128. [source, java]
  129. ----
  130. // Enable closing the tab
  131. tabsheet.getTab(tabComponent).setClosable(true);
  132. ----
  133. [[figure.layout.tabsheet.closing]]
  134. .TabSheet with Closable Tabs
  135. image::img/tabsheet-tabclose.png[]
  136. [[layout.tabsheet.closing.handling]]
  137. === Handling Tab Close Events
  138. You can handle closing tabs by implementing a custom
  139. [classname]#TabSheet.CloseHandler#. The default implementation simply calls
  140. [methodname]#removeTab()# for the tab to be closed, but you can prevent the
  141. close by not calling it. This allows, for example, opening a dialog window to
  142. confirm the close.
  143. [source, java]
  144. ----
  145. tabsheet.setCloseHandler(new CloseHandler() {
  146. @Override
  147. public void onTabClose(TabSheet tabsheet,
  148. Component tabContent) {
  149. Tab tab = tabsheet.getTab(tabContent);
  150. Notification.show("Closing " + tab.getCaption());
  151. // We need to close it explicitly in the handler
  152. tabsheet.removeTab(tab);
  153. }
  154. });
  155. ----
  156. ifdef::web[]
  157. [[layout.tabsheet.css]]
  158. == CSS Style Rules
  159. [source, css]
  160. ----
  161. .v-tabsheet {}
  162. .v-tabsheet-tabs {}
  163. .v-tabsheet-content {}
  164. .v-tabsheet-deco {}
  165. .v-tabsheet-tabcontainer {}
  166. .v-tabsheet-tabsheetpanel {}
  167. .v-tabsheet-hidetabs {}
  168. .v-tabsheet-scroller {}
  169. .v-tabsheet-scrollerPrev {}
  170. .v-tabsheet-scrollerNext {}
  171. .v-tabsheet-scrollerPrev-disabled{}
  172. .v-tabsheet-scrollerNext-disabled{}
  173. .v-tabsheet-tabitem {}
  174. .v-tabsheet-tabitem-selected {}
  175. .v-tabsheet-tabitemcell {}
  176. .v-tabsheet-tabitemcell-first {}
  177. .v-tabsheet-tabs td {}
  178. .v-tabsheet-spacertd {}
  179. ----
  180. The entire tabsheet has the [literal]#++v-tabsheet++# style. A tabsheet consists
  181. of three main parts: the tabs on the top, the main content pane, and decorations
  182. around the tabsheet.
  183. The tabs area at the top can be styled with [literal]#++v-tabsheet-tabs++#,
  184. [literal]#++v-tabsheet-tabcontainer++# and [literal]#++v-tabsheet-tabitem*++#.
  185. The style [literal]#++v-tabsheet-spacertd++# is used for any empty space after
  186. the tabs. If the tabsheet has too little space to show all tabs, scroller
  187. buttons enable browsing the full tab list. These use the styles
  188. [literal]#++v-tabsheet-scroller*++#.
  189. The content area where the tab contents are shown can be styled with
  190. [literal]#++v-tabsheet-content++#, and the surrounding decoration with
  191. [literal]#++v-tabsheet-deco++#.
  192. endif::web[]