Lakshitha Udara
Lakshitha Udara

Reputation: 153

GWT Tree Widget getElement() method not working as expected

I have a GWT UI Panel Created with Elements , and I need to add a GWT tree to the UI

I did

TreeItem department = new TreeItem("Packages");

      TreeItem salesDepartment = new TreeItem("Package1");
      TreeItem marketingDepartment = new TreeItem("Package2");
      TreeItem manufacturingDepartment = new TreeItem("Package3");

      TreeItem employee1 = new TreeItem("Flight");
      TreeItem employee2 = new TreeItem("Cruilse");
      TreeItem employee3 = new TreeItem("Flight");

      salesDepartment.addItem(employee1);
      salesDepartment.addItem(employee2);
      salesDepartment.addItem(employee3);

      TreeItem employee4 = new TreeItem("Cruise");
      TreeItem employee5 = new TreeItem("Hotel");      


      marketingDepartment.addItem(employee4);
      marketingDepartment.addItem(employee5);       

      TreeItem employee6 = new TreeItem("Cruise");
      TreeItem employee7 = new TreeItem("Hotel");

      manufacturingDepartment.addItem(employee6);
      manufacturingDepartment.addItem(employee7);

      department.addItem(salesDepartment);
      department.addItem(marketingDepartment);
      department.addItem(manufacturingDepartment);


      Tree tree = new Tree();
      tree.addItem(department);

      // consider that treeDiv is already added
      private Element treeDiv = DOM.createDiv();
      DOM.appendChild( treeDiv , tree.getElement() );

when I added the tree widget as a Element it shows only text inputs no expand buttons or anything,its not working as a tree just texts. please tell me is there any fix for this.

Upvotes: 0

Views: 112

Answers (1)

Colin Alworth
Colin Alworth

Reputation: 18331

  // consider that treeDiv is already added
  private Element treeDiv = DOM.createDiv();
  DOM.appendChild( treeDiv , tree.getElement() );

when I added the tree widget as a Element it shows only text inputs no expand buttons or anything,its not working as a tree just texts. please tell me is there any fix for this.

I'm not sure what that comment means, given that the tree was just created so could not have already been added, but even if that was a concern, I'm not sure why we must use the tree's element directly. If there are more pieces to this question you are skipping, please add them. For example: what is the treeDiv ever added to?


As a direct answer to the question, Don't Do This. Instead, use the widget's own add method, add widgets to other widgets, rather than directly manipulating the dom elements.

For example, if you are adding the tree to the <body> directly, this would look like this:

RootPanel.get().add(tree);

Or, if you already have a parent widget (called here panel), just add it directly to that parent:

panel.add(tree);

The reason for this is that the Widget wiring has its own event wiring that needs to be set up - to avoid memory leaks in the crappier browsers, it is important to make sure that event wiring is removed when the widgets are taken off the page. See http://www.gwtproject.org/articles/dom_events_memory_leaks_and_you.html for more details, but the short version is that after actually adding a widget's dom element to the greater dom, the widget's onAttach must be called, and any children widgets must also have their onAttach called as well. This is automatically done when adding a widget to another widget - you should stick to that API.

Upvotes: 1

Related Questions