Vaadin: How to iterate over tabs in a tab table?

In Vaadin, let's say I need to find a Tab in a TabSheet based on its name.

How to iterate through tabs in a Table to accomplish this?

+3
source share
3 answers

You can repeat the tabs and find them under the tab heading as follows:

Iterator<Component> i = tabs.getComponentIterator();
while (i.hasNext()) {
    Component c = (Component) i.next();
    Tab tab = tabs.getTab(c);
    if ("some_caption".equals(tab.getCaption())) {
         // found it
    }
}
+9
source

Vaadin 7.x is getComponentIterator()deprecated. Therefore, @eeq's answer is deprecated.

In a new way, his solution may look like this:

Iterator<Component> iterator = tabSheet.iterator();
while (iterator.hasNext()) {
    Component component = iterator.next();
    TabSheet.Tab tab = tabSheet.getTab(component);
    if ("some tab caption".equals(tab.getCaption())) {
        // Found it!!!
    }
}

But since TabSheet implements java.lang.Iterable<Component>, it might also look like this:

for (Component component : tabSheet) {
    TabSheet.Tab tab = tabSheet.getTab(component);
    if ("some tab caption".equals(tab.getCaption())) {
        // Found it!!!
    }
}

Or even in Java 8 style:

tabSheet.iterator().forEachRemaining(component -> {
    if ("some".equals(tabSheet.getTab(component).getCaption())) {
        // got it!!!
    }
});
+2
source

All Articles