Tabsheet component question

I have a tabsheet with 5 tabs in it. Each tab is using an externalResource to an integrated component (mostly flex/flash) The issue i’m having is that once vaadin loads the tab content, it keeps it loaded. I would like to destroy the content if the tab is no longer selected/visible and re-create it once the tab is re-selected. Does tabsheet support this or will I need to add my own logic to this?

After looking at the code more, I think the real question is can I remove the dom for an Embedded Component when its not visible and have it reload when it becomes visible?
Is this natively supported by tabsheet or embedded?

Did you try listening to the tab change event ?

You can then use this kind of logic :

  • On your current a tab, you do a removeComponent(embedded)
  • On the tab which will be displayed a addComponent(new Embedded(…));

That is what I ended up doing;

Here is the rough code, it’s not that pretty but you get the idea:

  1. Building the tabs
private void buildTabSheetTabs() {
		tabSheet.addTab(new VerticalLayout(), "Tab 1");
		tabSheet.addTab(new VerticalLayout(), "Tab 2");
		tabSheet.addTab(new VerticalLayout(), "Tab 3");
		tabSheet.addTab(new VerticalLayout(), "Tab 4");
		tabSheet.addTab(new VerticalLayout(), "Tab 5");
}
  1. Processing Change, removing all tabs and building a new one

		tabSheet.addListener(new TabSheet.SelectedTabChangeListener() {

			private static final long serialVersionUID = -8709201480371547797L;

			public void selectedTabChange(SelectedTabChangeEvent event) {
				removeAllTabComponents();
				Tab selectedTab = tabSheet.getTab(getSelectedTabIndex());
				buildTabContent((VerticalLayout) selectedTab.getComponent(),
						getSelectedTabIndex());
			}
		});

	private void removeAllTabComponents() {
		for (int i = 0; i < tabSheet.getComponentCount(); i++) {
			Tab tab = tabSheet.getTab(i);
			((VerticalLayout) tab.getComponent()).removeAllComponents();
		}
	}

	private int getSelectedTabIndex() {
		int index = -1;
		if (tabSheet.getComponentCount() > 0) {
			index = tabSheet.getTabPosition(tabSheet.getTab(tabSheet
					.getSelectedTab()));
		}
		return index;
	}

Building the tab content based on the index


	private void buildTabContent(VerticalLayout vLayout, int tabIndex) {
		vLayout.setMargin(false);
		vLayout.setSizeFull();
		vLayout.addComponent(buildTabBottomBar());

		Component tabContent = getTabContent(tabIndex);
		vLayout.addComponent(tabContent);
		vLayout.setExpandRatio(tabContent, 2);
	}