Search code examples
javagwtcomposite

Understanding composite in java or GWT


I have problem in understanding a composite.

In Frame class i have verticalPanel(vp) when vp is loaded , getAction and get Button is visible on vp

WHen click on button there is a getTree is executed and there is treeC class initialized where there is customised tree. and treeitem.

I want to use action object in class TreeC

How to do it.

Plz Help.

public class Frame{
public frame () {
    initWidget(getFramePanel());
}
Private VerticalalPanel getFramePanel() { 
    if (vp== null) {
        vp= new VerticalalPanel();
        vp.setSize("1442px", "750px");
        vp.add(getAction());// **are composites**
        vp.add(getButton) // **are composite**

    }
    return vp;

private Action getAction() {
        if (action == null) {
            action = new Action(); // In action class there are 7 buttons and 2 methods //setDisplayRepository(), and setDisplayFolder()
            action.setDisplayRepository();
        }
        return action;
    }
}
private Button getButton() {
        if (btn == null) {
            btn = new Button("Click");
            btnProperties.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    hp.add(getTree());
                }
            });
            btn.setSize("37px", "36px");

        }
        return btnProperties;
    }

private TreeCmis getTreeC() {
        if (treeC == null) {
            treeC = new TreeC();
            treeC.setWidth("360px");
        }
        return treeCmis;
    }
}

public class TreeC extends Composite{
private Tree repo;
//constructor
public TreeC {

createTree()
}
Void createTree(){
/* here i need to to use the object action declared in frame class
For using action.setDisplayfolder*/
}
}

Solution

  • The simplest way is:

    public class TreeC extends Composite{
      private Tree repo;
      private Action action;
      //constructor
      public TreeC(Action action) {
       this.action = action;
       createTree()
      }
      void createTree(){
       /* here i need to to use the object action declared in frame class
       For using action.setDisplayfolder*/
      }
    }
    

    When Create instance treeC = new TreeC(action);