2016-12-20 9 views
0

JTree에 노드를 동적으로 추가하는 데 문제가 있습니다.동적으로 JTree에 노드 추가

json 형식으로 RestAPI를 통해 JTree 정보를 수신합니다. 필요한 정보는 folderID와 folderName입니다.

folderID 구조는 같다 :

1 
1.1 
6.8 
7.1.1.1 
1.2 
etc. 

그래서 난 내 folderIDs의 포인트 내 nodelevel을 정의 할 필요가있다. 일부 코드를 검색하여이 코드를 찾았습니다. 내가 오류 메시지가 코드를 구현할 때

Dynamically add nodes in a JTree

는하지만 : 루트 당신이 내 문제에 대한 해결책을 찾을 수 있도록 할 수있는 변수

for(String s:list){ 
      String[] substr=s.split("\\."); 
      String parent=substr[0]; 
      for(int i=1;i<substr.length-1;i++){ 
       parent=parent+ "." + substr[i]; 
      } 
      DefaultMutableTreeNode node=null; 
      node=findparentnode(parent,**root**); 


      if(node==null) 
       **root**.add(new DefaultMutableTreeNode(s)); 
      else 
       node.add(new DefaultMutableTreeNode(s)); 

     } 

에 해결 될 수없는 이유는 무엇입니까?

답변

0

은 먼저 루트를 정의 할 수 있습니다

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root") 
JTree tree = new JTree(rootNode); 

그런 다음 루트 노드에 노드를 추가하여 JTree에 노드를 추가 할 시작할 수 있습니다.

+0

* * 루트 * * 연산자가 포함되어 있으며 컴파일되지 않습니다 – JEY

+0

추가했지만 다른 오류가 발생합니다. 유형에서 정적이 아닌 메소드 findparentnode (String, DefaultMutableTreeNode)에 대한 정적 참조를 만들 수 없습니다. 메인 – Boki

+0

노드 = ** findparentnode ** (부모, 루트); – Boki