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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ---
  2. title: TabSheet
  3. order: 9
  4. layout: page
  5. ---
  6. [[layout.tabsheet]]
  7. = 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[width=50%, scaledwidth=70%]
  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 Image(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 Image(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. [[layout.tabsheet.events]]
  76. == Tab Change Events
  77. Clicking on a tab selects it. This fires a
  78. [classname]#TabSheet.SelectedTabChangeEvent#, which you can handle by
  79. implementing the [classname]#TabSheet.SelectedTabChangeListener# interface. You
  80. can access the tabsheet of the event with [methodname]#getTabSheet()#, and find
  81. the new selected tab with [methodname]#getSelectedTab()#.
  82. You can programmatically select a tab with [methodname]#setSelectedTab()#, which
  83. also fires the [classname]#SelectedTabChangeEvent# (beware of recursive events).
  84. Reselecting the currently selected tab does not fire the event.
  85. Notice that when the first tab is added, it is selected and the change event is
  86. fired, so if you want to catch that, you need to add your listener before adding
  87. any tabs.
  88. [[layout.tabsheet.events.dynamic]]
  89. === Creating Tab Content Dynamically
  90. In the following example, we create the tabs as empty content layouts, and add
  91. the tab content dynamically when a tab is selected:
  92. [source, java]
  93. ----
  94. TabSheet tabsheet = new TabSheet();
  95. // Create tab content dynamically when tab is selected
  96. tabsheet.addSelectedTabChangeListener(
  97. new TabSheet.SelectedTabChangeListener() {
  98. public void selectedTabChange(SelectedTabChangeEvent event) {
  99. // Find the tabsheet
  100. TabSheet tabsheet = event.getTabSheet();
  101. // Find the tab (here we know it's a layout)
  102. Layout tab = (Layout) tabsheet.getSelectedTab();
  103. // Get the tab caption from the tab object
  104. String caption = tabsheet.getTab(tab).getCaption();
  105. // Fill the tab content
  106. tab.removeAllComponents();
  107. tab.addComponent(new Image(null,
  108. new ThemeResource("img/planets/"+caption+".jpg")));
  109. }
  110. });
  111. // Have some tabs
  112. String[] tabs = {"Mercury", "Venus", "Earth", "Mars"};
  113. for (String caption: tabs)
  114. tabsheet.addTab(new VerticalLayout(), caption,
  115. new ThemeResource("img/planets/"+caption+"_symbol.png"));
  116. ----
  117. [[layout.tabsheet.closing]]
  118. == Enabling and Handling Closing Tabs
  119. You can enable a close button for individual tabs with the
  120. [literal]#++closable++# property in the [classname]#TabSheet.Tab# objects.
  121. [source, java]
  122. ----
  123. // Enable closing the tab
  124. tabsheet.getTab(tabComponent).setClosable(true);
  125. ----
  126. [[figure.layout.tabsheet.closing]]
  127. .TabSheet with Closable Tabs
  128. image::img/tabsheet-tabclose.png[width=50%, scaledwidth=70%]
  129. [[layout.tabsheet.closing.handling]]
  130. === Handling Tab Close Events
  131. You can handle closing tabs by implementing a custom
  132. [classname]#TabSheet.CloseHandler#. The default implementation simply calls
  133. [methodname]#removeTab()# for the tab to be closed, but you can prevent the
  134. close by not calling it. This allows, for example, opening a dialog window to
  135. confirm the close.
  136. [source, java]
  137. ----
  138. tabsheet.setCloseHandler(new CloseHandler() {
  139. @Override
  140. public void onTabClose(TabSheet tabsheet,
  141. Component tabContent) {
  142. Tab tab = tabsheet.getTab(tabContent);
  143. Notification.show("Closing " + tab.getCaption());
  144. // We need to close it explicitly in the handler
  145. tabsheet.removeTab(tab);
  146. }
  147. });
  148. ----
  149. [[layout.tabsheet.css]]
  150. == CSS Style Rules
  151. [source, css]
  152. ----
  153. .v-tabsheet {}
  154. .v-tabsheet-tabs {}
  155. .v-tabsheet-content {}
  156. .v-tabsheet-deco {}
  157. .v-tabsheet-tabcontainer {}
  158. .v-tabsheet-tabsheetpanel {}
  159. .v-tabsheet-hidetabs {}
  160. .v-tabsheet-scroller {}
  161. .v-tabsheet-scrollerPrev {}
  162. .v-tabsheet-scrollerNext {}
  163. .v-tabsheet-scrollerPrev-disabled{}
  164. .v-tabsheet-scrollerNext-disabled{}
  165. .v-tabsheet-tabitem {}
  166. .v-tabsheet-tabitem-selected {}
  167. .v-tabsheet-tabitemcell {}
  168. .v-tabsheet-tabitemcell-first {}
  169. .v-tabsheet-tabs td {}
  170. .v-tabsheet-spacertd {}
  171. ----
  172. The entire tabsheet has the [literal]#++v-tabsheet++# style. A tabsheet consists
  173. of three main parts: the tabs on the top, the main content pane, and decorations
  174. around the tabsheet.
  175. The tabs area at the top can be styled with [literal]#++v-tabsheet-tabs++#,
  176. [literal]#++v-tabsheet-tabcontainer++# and [literal]#++v-tabsheet-tabitem*++#.
  177. The style [literal]#++v-tabsheet-spacertd++# is used for any empty space after
  178. the tabs. If the tabsheet has too little space to show all tabs, scroller
  179. buttons enable browsing the full tab list. These use the styles
  180. [literal]#++v-tabsheet-scroller*++#.
  181. The content area where the tab contents are shown can be styled with
  182. [literal]#++v-tabsheet-content++#, and the surrounding decoration with
  183. [literal]#++v-tabsheet-deco++#.