2 Replies Latest reply on Feb 1, 2008 4:50 AM by bm97

    How can expand the new node just added

      I use below codes,adding is success, but current node still not expanded

      MyTreeNode newNode = new MyTreeNode(Long.valueOf(-1));
       newNode.setNodeName("<NA>");
       MyTreeNode sNode = (MyTreeNode) selectedNode;
       sNode.addChild(newNode);
      
       TreeRowKey keys = (TreeRowKey)tree.getRowKey();
      // tree.setRowKey(null);
      // tree.queueEvent(new AjaxEvent(tree.getNodeFacet()));
      // tree.setRowKey(keys);
      
       Iterator iterator= keys.iterator();
       ArrayList al = new ArrayList();
       while (iterator.hasNext()){
       Long lon = (Long)iterator.next();
       al.add(lon);
       }
       al.add(newNode.getNodeid());
       ListRowKey newKeys = new ListRowKey(al);
       tree.setRowKey(newKeys);
       TreeState state = (TreeState) tree.getComponentState();
       try {
       state.expandNode(newKeys);
       } catch (IOException e) {
       e.printStackTrace();
       }


        • 1. Re: How can expand the new node just added

          It's ok now!

          public void expandNode(MyTreeNode node) {
           ArrayList<Long> path = new ArrayList<Long>();
           while (node != null) {
           path.add(node.getNodeid());
           node = (MyTreeNode) node.getParent();
           }
           Collections.reverse(path);
           path.remove(0); // dont expand rootnode, it is not displayed
          
           for (int i = 0; i < path.size(); i++) {
           ArrayList<Long> list = new ArrayList<Long>();
           for (int n = 0; n <= i; n++) {
           list.add(path.get(n));
           }
          
           ListRowKey key = new ListRowKey(list);
           try {
           ((HtmlTree) tree).queueNodeExpand(key);
           } catch (IOException e) {
           e.printStackTrace();
           }
           }
           }


          • 2. Re: How can expand the new node just added

            Thanks, it works.

            But, when you don't want to get the first child of the node expanded, remove the deepest node too.

            public final void expandNode(AbstractTreeNode node) {
             final ArrayList<Object> path = new ArrayList<Object>();
             while (node != null) {
             path.add(node.getNodeId());
             node = (AbstractTreeNode) node.getParent();
             }
            
             // ADDED
             path.remove(0); //dont expand first child
             // ADDED
            
             Collections.reverse(path);
             path.remove(0); // dont expand rootnode, it is not displayed
            
             for (int i = 0; i < path.size(); i++) {
             final ArrayList<Object> list = new ArrayList<Object>();
             for (int n = 0; n <= i; n++) {
             list.add(path.get(n));
             }
            
             final ListRowKey key = new ListRowKey(list);
             try {
             ((HtmlTree) this.tree).queueNodeExpand(key);
             } catch (final IOException e) {
             e.printStackTrace();
             }
             }
             }