TabSheet

Live Demo

The TabSheet is a multicomponent container that allows switching between the components with “tabs”. The tabs are organized as a tab bar at the top of the tab sheet. Clicking on a tab opens its contained component in the main display area of the layout. If there are more tabs than fit in the tab bar, navigation buttons will appear.

tabsheet example1

A Simple TabSheet Layout

Adding Tabs

You add new tabs to a tab sheet with the addTab() method. The simple version of the method takes as its parameter the root component of the tab. You can use the root component to retrieve its corresponding Tab object. Typically, you put a layout component as the root component.

You can also give the caption and the icon as parameters for the addTab() method. The following example demonstrates the creation of a simple tab sheet, where each tab shows a different Label component. The tabs have an icon, which are (in this example) loaded as Java class loader resources from the application.

Java

  1. TabSheet tabsheet = new TabSheet();
  2. layout.addComponent(tabsheet);
  3. // Create the first tab
  4. VerticalLayout tab1 = new VerticalLayout();
  5. tab1.addComponent(new Image(null,
  6. new ThemeResource("img/planets/Mercury.jpg")));
  7. tabsheet.addTab(tab1, "Mercury",
  8. new ThemeResource("img/planets/Mercury_symbol.png"));
  9. // This tab gets its caption from the component caption
  10. VerticalLayout tab2 = new VerticalLayout();
  11. tab2.addComponent(new Image(null,
  12. new ThemeResource("img/planets/Venus.jpg")));
  13. tab2.setCaption("Venus");
  14. tabsheet.addTab(tab2).setIcon(
  15. new ThemeResource("img/planets/Venus_symbol.png"));
  16. ...

Tab Objects

Each tab in a tab sheet is represented as a Tab object, which manages the tab caption, icon, and attributes such as hidden and visible. You can set the caption with setCaption() and the icon with setIcon(). If the component added with addTab() has a caption or icon, it is used as the default for the Tab object. However, changing the attributes of the root component later does not affect the tab, but you must make the setting through the Tab object. The addTab() returns the new Tab object, so you can easily set an attribute using the reference.

Java

  1. // Set an attribute using the returned reference
  2. tabsheet.addTab(myTab).setCaption("My Tab");

Disabling and Hiding Tabs

A tab can be disabled by setting setEnabled(false) for the Tab object, thereby disallowing selecting it.

A tab can be made invisible by setting setVisible(false) for the Tab object. The hideTabs() method allows hiding the tab bar entirely. This can be useful in tabbed document interfaces (TDI) when there is only one tab.

Tab Change Events

Clicking on a tab selects it. This fires a TabSheet.SelectedTabChangeEvent, which you can handle by implementing the TabSheet.SelectedTabChangeListener interface. You can access the tabsheet of the event with getTabSheet(), and find the new selected tab with getSelectedTab().

You can programmatically select a tab with setSelectedTab(), which also fires the SelectedTabChangeEvent (beware of recursive events). Reselecting the currently selected tab does not fire the event.

Notice that when the first tab is added, it is selected and the change event is fired, so if you want to catch that, you need to add your listener before adding any tabs.

Creating Tab Content Dynamically

In the following example, we create the tabs as empty content layouts, and add the tab content dynamically when a tab is selected:

Java

  1. TabSheet tabsheet = new TabSheet();
  2. // Create tab content dynamically when tab is selected
  3. tabsheet.addSelectedTabChangeListener(
  4. new TabSheet.SelectedTabChangeListener() {
  5. public void selectedTabChange(SelectedTabChangeEvent event) {
  6. // Find the tabsheet
  7. TabSheet tabsheet = event.getTabSheet();
  8. // Find the tab (here we know it's a layout)
  9. Layout tab = (Layout) tabsheet.getSelectedTab();
  10. // Get the tab caption from the tab object
  11. String caption = tabsheet.getTab(tab).getCaption();
  12. // Fill the tab content
  13. tab.removeAllComponents();
  14. tab.addComponent(new Image(null,
  15. new ThemeResource("img/planets/"+caption+".jpg")));
  16. }
  17. });
  18. // Have some tabs
  19. String[] tabs = {"Mercury", "Venus", "Earth", "Mars"};
  20. for (String caption: tabs)
  21. tabsheet.addTab(new VerticalLayout(), caption,
  22. new ThemeResource("img/planets/"+caption+"_symbol.png"));

Enabling and Handling Closing Tabs

You can enable a close button for individual tabs with the closable property in the TabSheet.Tab objects.

Java

  1. // Enable closing the tab
  2. tabsheet.getTab(tabComponent).setClosable(true);

tabsheet tabclose

TabSheet with Closable Tabs

Handling Tab Close Events

You can handle closing tabs by implementing a custom TabSheet.CloseHandler. The default implementation simply calls removeTab() for the tab to be closed, but you can prevent the close by not calling it. This allows, for example, opening a dialog window to confirm the close.

Java

  1. tabsheet.setCloseHandler(new CloseHandler() {
  2. @Override
  3. public void onTabClose(TabSheet tabsheet,
  4. Component tabContent) {
  5. Tab tab = tabsheet.getTab(tabContent);
  6. Notification.show("Closing " + tab.getCaption());
  7. // We need to close it explicitly in the handler
  8. tabsheet.removeTab(tab);
  9. }
  10. });

CSS Style Rules

CSS

  1. .v-tabsheet {}
  2. .v-tabsheet-tabs {}
  3. .v-tabsheet-content {}
  4. .v-tabsheet-deco {}
  5. .v-tabsheet-tabcontainer {}
  6. .v-tabsheet-tabsheetpanel {}
  7. .v-tabsheet-hidetabs {}
  8. .v-tabsheet-scroller {}
  9. .v-tabsheet-scrollerPrev {}
  10. .v-tabsheet-scrollerNext {}
  11. .v-tabsheet-scrollerPrev-disabled{}
  12. .v-tabsheet-scrollerNext-disabled{}
  13. .v-tabsheet-tabitem {}
  14. .v-tabsheet-tabitem-selected {}
  15. .v-tabsheet-tabitemcell {}
  16. .v-tabsheet-tabitemcell-first {}
  17. .v-tabsheet-tabs td {}
  18. .v-tabsheet-spacertd {}

The entire tabsheet has the v-tabsheet style. A tabsheet consists of three main parts: the tabs on the top, the main content pane, and decorations around the tabsheet.

The tabs area at the top can be styled with v-tabsheet-tabs, v-tabsheet-tabcontainer and v-tabsheet-tabitem*.

The style v-tabsheet-spacertd is used for any empty space after the tabs. If the tabsheet has too little space to show all tabs, scroller buttons enable browsing the full tab list. These use the styles v-tabsheet-scroller*.

The content area where the tab contents are shown can be styled with v-tabsheet-content, and the surrounding decoration with v-tabsheet-deco.