2011-03-08 2 views
0

모든 자식 노드와 속성이 다음 형식으로있는 단일 노드가 있습니다.전체 내용을 가진 단일 노드 요소를 사용하여 JTree를 표시하십시오.

node = Root[ 
     attributes = {rootattribute1, rootattribute2,...}, 
     value = [100, 
       childNode1 
       [ 
       attributes = {childNode2att1,.....} 
       value = [1001] 
       ] 

       childNode2 
       [ 
       attributes = {childNode2attributes,.....} 
       value = [1001] 
       ] ......... and some other childnodes like this 
       ] 

나는 Jtree tree = new Jtree (node)를 사용할 때; 트리의 단일 행 내에서 이러한 모든 세부 정보를 보여주는 트리에 대한 단일 rootelement를 작성합니다.

대신 중첩 된 자식 노드 및 atrribute 값이있는 올바른 계층 구조로 트리를 표시하려고합니다. 거기에 어떤 inbuilt 방법이 있나요?

이 작업을 수행 할 inbuilt 메서드가 없으면 어떻게 코드를 작성합니까?

PS : 위에 표시된 노드 내용은 동적이며 정적이 아닙니다.

답변

1

당신이 좋아하는 뭔가를 시작할 수 있습니다

import javax.swing.* 
import javax.swing.tree.* 

class Root { 
    def attributes = [] 
    def children = [] 
    def value = 0 

    def String toString() { 
     "[${value}] attributes: ${attributes} children: ${children}" 
    } 
} 

def createTreeNode(node) { 
    def top = new DefaultMutableTreeNode(node.value) 
    for (attr in node.attributes) { 
     top.add(new DefaultMutableTreeNode(attr)) 
    } 
    for (child in node.children) { 
     top.add(createTreeNode(child)) 
    } 
    top 
} 

root = new Root(
    attributes: ['rootattribute1', 'rootattribute2'], 
    value: 100, 
    children: [ 
     new Root(
      attributes: ['childNode2att1'], 
      value: 1001), 
     new Root(
      attributes: ['childNode2attributes'], 
      value: 1002),  
    ]) 


frame = new JFrame('Tree Test') 
frame.setSize(300, 300) 
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE 
jtree = new JTree(createTreeNode(root)) 
frame.add(jtree) 
frame.show() 

의 JTree은 정교한 구성 요소입니다 - 당신의 정확한 요구에 맞게 나무를 사용자 정의하는 방법에 대한 자세한 내용은 JTree Swing Tutorial을 참조하십시오.

+0

많은 많은 감사합니다. 코드가 도움이되었습니다. –

관련 문제