Repository: gwtmaterialdesign/gwt-material-addins
Issue: Adding MaterialTreeItem programmatically does not set owning MaterialTree.

The onLoad handler and the addItem method do not follow the same strategy to add items to the tree. 

Changed files:
src/main/java/gwt/material/design/addins/client/tree/MaterialTree.java, src/test/java/gwt/material/design/addins/client/MaterialTreeTest.java, src/main/java/gwt/material/design/addins/client/tree/MaterialTreeItem.java

diff --git a/src/main/java/gwt/material/design/addins/client/tree/MaterialTree.java b/src/main/java/gwt/material/design/addins/client/tree/MaterialTree.java
index e3c7837d..2c5669a3 100644
--- a/src/main/java/gwt/material/design/addins/client/tree/MaterialTree.java
+++ b/src/main/java/gwt/material/design/addins/client/tree/MaterialTree.java
@@ -110,8 +110,8 @@ public class MaterialTree extends MaterialWidget implements HasCloseHandlers<Mat
     @Override
     protected void add(Widget child, com.google.gwt.user.client.Element container) {
         if (child instanceof MaterialTreeItem) {
-            ((MaterialTreeItem) child).setTree(this);
             super.add(child, container);
+            ((MaterialTreeItem) child).setTree(this);
         } else {
             throw new IllegalArgumentException("MaterialTree can only contain MaterialTreeItem");
         }
@@ -120,8 +120,8 @@ public class MaterialTree extends MaterialWidget implements HasCloseHandlers<Mat
     @Override
     protected void insert(Widget child, com.google.gwt.user.client.Element container, int beforeIndex, boolean domInsert) {
         if (child instanceof MaterialTreeItem) {
-            ((MaterialTreeItem) child).setTree(this);
             super.insert(child, container, beforeIndex, domInsert);
+            ((MaterialTreeItem) child).setTree(this);
         } else {
             throw new IllegalArgumentException("MaterialTree can only contain MaterialTreeItem");
         }
diff --git a/src/main/java/gwt/material/design/addins/client/tree/MaterialTreeItem.java b/src/main/java/gwt/material/design/addins/client/tree/MaterialTreeItem.java
index 34e81072..1752c69a 100644
--- a/src/main/java/gwt/material/design/addins/client/tree/MaterialTreeItem.java
+++ b/src/main/java/gwt/material/design/addins/client/tree/MaterialTreeItem.java
@@ -273,18 +273,18 @@ public class MaterialTreeItem extends AbstractIconButton implements HasImage, Ha
 
     @Override
     protected void add(Widget child, com.google.gwt.user.client.Element container) {
+        super.add(child, container);
         if (child instanceof MaterialTreeItem) {
             ((MaterialTreeItem) child).setTree(getTree());
         }
-        super.add(child, container);
     }
 
     @Override
     protected void insert(Widget child, com.google.gwt.user.client.Element container, int beforeIndex, boolean domInsert) {
+        super.insert(child, container, beforeIndex, domInsert);
         if (child instanceof MaterialTreeItem) {
             ((MaterialTreeItem) child).setTree(getTree());
         }
-        super.insert(child, container, beforeIndex, domInsert);
     }
 
     @Override
diff --git a/src/test/java/gwt/material/design/addins/client/MaterialTreeTest.java b/src/test/java/gwt/material/design/addins/client/MaterialTreeTest.java
index 35440b78..41e8bd66 100644
--- a/src/test/java/gwt/material/design/addins/client/MaterialTreeTest.java
+++ b/src/test/java/gwt/material/design/addins/client/MaterialTreeTest.java
@@ -26,7 +26,6 @@ import gwt.material.design.addins.client.base.constants.AddinsCssName;
 import gwt.material.design.addins.client.tree.MaterialTree;
 import gwt.material.design.addins.client.tree.MaterialTreeItem;
 import gwt.material.design.client.base.MaterialWidget;
-import gwt.material.design.client.constants.CssName;
 import gwt.material.design.client.constants.IconType;
 import gwt.material.design.client.ui.MaterialIcon;
 import gwt.material.design.client.ui.MaterialImage;
@@ -45,6 +44,42 @@ public class MaterialTreeTest extends MaterialAddinsTest {
         checkStructure(tree);
         checkSelectedItem(tree);
         checkExpandAndColapse(tree);
+        checkCreateItemAndSelect(tree);
+        checkInsertItemAndSelect(tree);
+        checkCreateSubItemAndSelect(tree);
+        checkInsertSubItemAndSelect(tree);
+    }
+
+    protected void checkCreateItemAndSelect(MaterialTree tree) {
+        MaterialTreeItem treeItem = new MaterialTreeItem();
+        treeItem.setText("Child");
+        tree.add(treeItem);
+        treeItem.select();
+    }
+
+    protected void checkInsertItemAndSelect(MaterialTree tree) {
+        MaterialTreeItem treeItem = new MaterialTreeItem();
+        treeItem.setText("Child");
+        tree.insert(treeItem, 0);
+        treeItem.select();
+    }
+
+    protected void checkCreateSubItemAndSelect(MaterialTree tree) {
+        MaterialTreeItem root = new MaterialTreeItem("Root");
+        tree.add(root);
+        MaterialTreeItem child = new MaterialTreeItem("Child");
+        root.add(child);
+        tree.expand();
+        child.select();
+    }
+
+    protected void checkInsertSubItemAndSelect(MaterialTree tree) {
+        MaterialTreeItem root = new MaterialTreeItem("Root");
+        tree.add(root);
+        MaterialTreeItem child = new MaterialTreeItem("Child");
+        root.insert(child, 0);
+        tree.expand();
+        child.select();
     }
 
     protected <T extends MaterialTree> void checkExpandAndColapse(T tree) {