2011-02-03 56 views
1
TreeModel model = new TreeModel() { 

    Node[] sillyTree = { 

     new Node("X", new Node[] { 
      new Node("Child 1", new Node[] { 
      }), 
      new Node("Child 2", new Node[] { 
      }), 
      new Node("Child 3", new Node[] { 
      }), 
     }), 
     new Node("Y", new Node[] { 
      new Node("A", new Node[] { 
      }) 
     }), 
     new Node("Z", new Node[] { 
      new Node("A", new Node[] { 
      }), 
     }), 
    }; 

我想作爲dynacially創建一個節點,上述靜態 示例代碼plz幫助動態創建LWUIT節點的樹狀視圖

+0

http://www.java.net/forum/topic/mobile-embedded/lwuit/tree-component。我試着用這個示例但不能運行,我只有空指針異常 – JohnRaja 2011-02-03 12:46:16

回答

0

見UIDemo應用TreeDemo類。這裏動態檢索文件根目錄中的值並將其添加到向量中。請參閱TreeDemo類中的FileTreeModel內部類。您可以按照相同的方式添加動態值。

0

這可能幫助 -

class Node { 
    Vector children; 
    String value; // Name of the node…. 

    public Node(String value, Vector children) { 
     this.children = children; 
     this.value = value; 
    } 

    public String toString() { 
     return value; 
    } 
} 

而且 -

Node[] nodeArray = generateRootNode(); 

    TreeModel model = new TreeModel() { 

    Node[] sillyTree = nodeArray; 

       public Vector getChildren(Object parent) { 
        Node n = (Node) parent; 
        Object[] nodes = null; 
        Vector v = new Vector(); 
        if (parent == null) { 
         nodes = sillyTree; 
        } else { 
         v = n.children; 
        } 
        if (nodes != null) { 
         for (int iter = 0; iter < nodes.length; iter++) { 
          v.addElement(nodes[iter]); 
         } 
        } 
        return v; 
       } 

       public boolean isLeaf(Object node) { 
        boolean returnValue = false; 
        try { 
         Node n = (Node) node; 
         returnValue = n.children == null || n.children.size() == 0; 
        } catch (ClassCastException ex) { 
         // It means its a child node or a leaf... 
          returnValue = true; 
        } 
        return returnValue; 
       } 
      };