Search code examples
javazk

How to use ZK getFellow() method?


I am passing a bunch of tabs from a zul file to a java file like so:

tabs.zul

<tabs>
  <tab id="tab1" label="Tab1"> </tab>
  <tab id="tab2" label="Tab2"> </tab>
</tabs>
<zscript>
  testTabs = new TestTabs();
  Tab[] tabs = {tab1, tab2}
  testTabs.registerTabs(tabs)
</zscript>

TestTabs.java

public class TestTabs {
  ....
  private HashMap<String,Tab> tabMap;

    void registerTabs (Tab[] tabs) {
      this.tabMap = new HashMap<String,Tab>();
      for (Tab t: tabs) {
        this.tabMap.put(t.getId(),t);
      }
    }

   if(condition) {
     tabMap.get("tab1").setVisible(true);
     tabMap.get("tab2").setVisible(true);
   }  

}

Now, I guess using Hashmaps to access a tab is a roundabout way. Using a getFellow(String id) method to access a tab would be much simpler, right ? But, I am not sure how to implement that. Can someone help me with this?

Thanks, Sony


Solution

  • There are several ways to do that:

    1. Extend org.zkoss.zul.Window in your class and link it in your zul file like this:
      <window id="myWindow" use="package.to.your.ClassThatExtendsWindow">
      <!-- your tabs go here -->
      </window>
      Then in your class you can use Tab tab1 = (Tab) this.getFellow("tab1");
    2. Extend org.zkoss.zk.ui.util.GenericForwardComposer and link it in your zul like this:
      <window id="myWindow" apply="package.to.your.ClassThatExtendsGenericForwardComposer">
      <!-- your tabs go here -->
      </window>
      Then in your class declare private Tab tab1; and you can use it right away.

    Note the differente bewtween the use and apply keywords. If you use the second approach, make sure that the name of your variable matches the id of your component ("tab1").