1 Reply Latest reply on Oct 28, 2008 1:00 AM by Dana Heath

    Tree how to get TreeNode?

    Stateless Bean Novice

      Hi,
      Can anyone help me to get working with Tree component?

      What I want to do? I got records in db and want to show them in tree, and moving on tree I want to get some record-node and use it?

      Here is my overrided TreeNode class with my db record (id, info etc.)

      public class TreeNodeImpl implements TreeNode {
      
       private static final long serialVersionUID = -5498990493803705085L;
       private Object data;
       private TreeNode parent;
       public Dictionary dictionary;
      
       private Map childrenMap = new LinkedHashMap();
      
       public Object getData() {
       return data;
       }
      
       public TreeNodeImpl getChild(Object identifier) {
       return (TreeNodeImpl) childrenMap.get(identifier);
       }
      
       public void addChild(Object identifier, TreeNode child) {
       child.setParent(this);
       childrenMap.put(identifier, child);
       }
      
       public void removeChild(Object identifier) {
       TreeNode treeNode = (TreeNode) childrenMap.remove(identifier);
       if (treeNode != null) {
       treeNode.setParent(null);
       }
       }
      
       public void setData(Object data) {
       this.data = data;
       }
      
       public TreeNode getParent() {
       return parent;
       }
      
       public void setParent(TreeNode parent) {
       this.parent = parent;
       }
      
       public Iterator getChildren() {
       return childrenMap.entrySet().iterator();
       }
      
       public boolean isLeaf() {
       return childrenMap.isEmpty();
       }
      
       public Dictionary getDictionary() {
       return dictionary;
       }
      
       public void setDictionary(Dictionary dictionary) {
       this.dictionary = dictionary;
       }
      
      



      Here is my tree "constructor"

       private void init() {
       int counter = 1;
      
       TreeNodeImpl nodeImpl = new TreeNodeImpl();
       nodeImpl.setData("a");
       node.addChild(new Integer(counter), nodeImpl);
       counter++;
       TreeNodeImpl nodeImpl6 = new TreeNodeImpl();
       nodeImpl6.setData("b");
       node.addChild(new Integer(counter), nodeImpl6);
       }
      


      my getter
       public TreeNode getTreeNode() {
       if (rootNode == null) {
       init();
       }
       return rootNode;
      }
      


      and my listener

       }
      
       public void processSelection(NodeSelectedEvent event) {
       UITree tree = (UITree) event.getComponent();
       Object a = (Object)tree.getTreeNode().getData();
       TreeNode elem = rootNode.getChild(a);
      
       System.out.println(a.toString());
       }
      


      and here is my problem, why I get only string "a" and "b", can anyone help me what to do to get my object Dictionary?

      Or can anyone show me own source code how to get any own object from node?

        • 1. Re: Tree how to get TreeNode?
          Dana Heath Newbie

          It could be that you have created a TreeNodeImpl class with the same name as the Richfaces TreeNodeImpl class and the code is finding the Richfaces version first, which makes sense since it looks like you're passing in a string "a" to the setData method.

          Try renaming your class something else