开发者

How to find if a Tab is already opened in Vaadin and setting it active instead of creating a new one

In Vaadin, once you have a TabSheet, and some tabs are already opened, you don't want the same Tab, containing the same content to be opened many times. How can I check that a Tab is already open开发者_如何学Pythoned and set it to as the selected one?


The Vaadin's TabSheet checks by default, if a component already dded by comparing the component's hashcode.

It means you have to implement the method hashCode() in the component you want to add to the TabSheet.


Using iterator in tab component, and compare with name.

Iterator<Component> i = tabSheet.getComponentIterator();
    while (i.hasNext()) {
        Component c = (Component) i.next();
        Tab tab = tabSheet.getTab(c);
        if (name.equals(tab.getCaption())) {
            tabSheet.setSelectedTab(c);
            return;
        }
   }


In new Vaadin it could be solved in this way:

Component component = //...
for (Component c : tabSheet) {
    if (c.equals(component)) {
        // duplicate...
    }
}

But as @CRH mentioned before, you should take care of equal() and hashCode() correctness.


In my case, it could be done as follow:

Component cmp = /*you component with a description*/;


for (Component c : tabSheet) {
 if (c.getDescription().equals(cmp.getDescription())) {
     System.out.println("Equals Equals");
     return;
 }


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜