TabSheet selected tab caption is null

The .getSelectedTab().getCaption() method seems to return null, even when the Tab component has a caption. Here’s an example:

public class VaadintabApplication extends Application {
	public void init() {
		Window mainWindow = new Window("Tab Test");
        TabSheet tabsheet = new TabSheet();

        VerticalLayout vla = new VerticalLayout();
        tabsheet.addTab(vla);
        tabsheet.getTab(vla).setCaption("Tab 1");

        VerticalLayout vlb = new VerticalLayout();
        tabsheet.addTab(vlb);
        tabsheet.getTab(vlb).setCaption("Tab 2");

        tabsheet.addListener(new TabSheet.SelectedTabChangeListener() {
            public void selectedTabChange(SelectedTabChangeEvent event) {
                System.out.println(event.getTabSheet().getSelectedTab().getCaption());
            }
        });		
		mainWindow.addComponent(tabsheet);
		setMainWindow(mainWindow);
	}

}

Am I missing something?

Allen

You’ve stumbled upon a gotcha in Vaadin: in TabSheet, you have not only Tabs, but also components that represent those tabs.

What you’re getting from TabSheet.getSelectedTab() is an instance of Component, and you’re asking for that component’s caption. But you’re setting the caption for the Tab object that represents the tab for the component. Two different things, unfortunately.

To overcome this, set the caption for the component you add to the TabSheet. It will automatically be copied to the Tab object from there.

Example:

TabSheet tabsheet = new TabSheet();
VerticalLayout vla = new VerticalLayout();
vla.setCaption("Tab 1");
tabsheet.addTab(vla);

Now your listener should get the caption correctly.

Thanks Jouni! That fixed it.

Allen

But it is stupid that one can not get access into Tab via getSelectedTab - Idea to use caption on component is not good, if one need use only caption of Tab. Caption of component comes up problem of what to do caption which should no show in page. Better idea is create event which gives Tab and Component instead just component.

Hello,
Is there any chance to get this strange and confusing implementation fixed? It costs endless number of developers like me time to debug and research the problem that shouldn’t have been there on first place. In my case I added several tabs by calling
tabs.addTab(serviceGeneralTab, “Services”);

and silly me, I expected by calling
event.getTabSheet().getSelectedTab().getCaption()
to be able to read the caption.
After all - the method is called getSelectedTab and not getSelectedTabComponent…
Why don’t you just make an extra method TabSheet.getSelectedTabComponent() instead?
Please at least add the information and the hack that you describe here to the TabSheet online documentation, there is no need to search on the forum for problems in order to discover how a component works…

Best Regards,
mbe

Be patient, mbe. This thread has been open for just 7 years ago. It must be almost done… xD

UPDATE:

Cheers! There’s an easy, intuitive (irony) way to get the tab caption without having to set a caption to the inner layout of the tab. First you get the component via getSelectedTab(), then you get the tab itself with TabSheet.getTab(component) and then, finally, it seems you can get the caption:

Component c = myTabSheet.getSelectedTab();
Tab tb = myTabSheet.getTab(c);
String capt = tb.getCaption();

Why was it engineered this way? Vaadin knows…

Thank a lot
Pere Pasqual

Thanks Pere Pasqual, Nice quick answer.

I suppose the only issue is that the method getSelectedTab(), is actually getSelectedTabComponent or getSelectedTabLayoutComponent.

If you’re reading this, consider implementing your own accordion if you’re still using Vaadin 7. The Vaadin native one is a monster.