2017-01-27 4 views
-1

나는이 테이블이 :배열 데이터에서 JTree 구조를 만드는 방법은 무엇입니까?

+---------------------+ 
| Name | Type | Month | 
+---------------------+ 
| John | xyz | March | 
+---------------------+ 
| Joe | xyz | March | 
+---------------------+ 
| Nick | abc | March | 
+---------------------+ 
| Phil | abc | March | 
+---------------------+ 

나는이 방법에서의 JTree에 그 기록을 보여주고 싶습니다

March 
| xyz 
| | John 
| | Joe 
| abc 
    | Nick 
    | Phil 

내가 어떻게 할 수 있습니다

?

필자는 이것에 관한 많은 문서를 발견했지만 알 수없는 길이의 배열에서이를 수행하는 방법을 여전히 이해할 수 없습니다.

저는 UCanAccess를 사용하여 모든 레코드를 선택하고 모두 배열에 저장하기 때문에이 질문을하고 싶습니다.

구조에 노드를 추가하는 가장 효율적인 방법은 무엇일까요? (300+ 요소를 저장하는 것으로 간주)

+0

UCanAccess는 JDBC 인터페이스를 제공합니다. 그것은 배열에 레코드를 저장하지 않습니다! – jamadei

+0

@jamadei이 (가) 편집했습니다. 그럼 대답을 아시나요? – valbuxvb

+0

* "길이를 알 수없는 배열에서이 작업을 수행하는 방법을 여전히 이해할 수 없습니다"* - 배열의 요소를 반복 할 수 있음을 알고 있습니다 (http://stackoverflow.com/q/2331509/2144390). , 권리? –

답변

1

유형 또는 월이 변경되면 배열을 반복하고 트리 노드를 추가합니다. 이것은 매우 단순한 예입니다. 배열을 설명하는 Test2.java 클래스를 사용합니다.

public class Test2 { 
private String month; 
private String type; 
private String name; 


public Test2(String month, String type, String name) { 
    setMonth(month); 
    setType(type); 
    setName(name); 
} 

/** 
* @return the month 
*/ 
public String getMonth() { 
    return month; 
} 

/** 
* @param month the month to set 
*/ 
public void setMonth(String month) { 
    this.month = month; 
} 

/** 
* @return the Type 
*/ 
public String getType() { 
    return type; 
} 

/** 
* @param Type the Type to set 
*/ 
public void setType(String type) { 
    this.type = type; 
} 

/** 
* @return the name 
*/ 
public String getName() { 
    return name; 
} 

/** 
* @param name the name to set 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

} 

을 그리고이 클래스는 JFrame의에서의 JTree를 표시합니다 : 확인, 데이터가 올바른 정렬 된 방식으로 제공

import java.awt.BorderLayout; 
import java.util.ArrayList; 
import java.util.Iterator; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 


public class Test1 extends JFrame { 

public Test1() { 
    super("Test"); 

    ArrayList<Test2> list = new ArrayList(); 
    list.add(new Test2("March", "xyz", "John")); 
    list.add(new Test2("March", "xyz", "Joe")); 
    list.add(new Test2("March", "abc", "Nick")); 
    list.add(new Test2("March", "abc", "Phil")); 

    Iterator iter = list.iterator(); 
    String prevMonth = ""; 
    String prevType = ""; 

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("List"); 
    DefaultMutableTreeNode month = null; 
    DefaultMutableTreeNode type = null; 

    while (iter.hasNext()) { 
     Test2 t = (Test2) iter.next(); 
     if (!t.getMonth().equals(prevMonth)) {     
      if (month != null) {     
       top.add(month); 
      } 
      month = new DefaultMutableTreeNode(t.getMonth()); 
      prevMonth = t.getMonth(); 

     } 
     if (!t.getType().equals(prevType)) {     
      if (type != null) {      
       month.add(type); 
      } 
      type = new DefaultMutableTreeNode(t.getType()); 
      prevType = t.getType(); 
     }    
     type.add(new DefaultMutableTreeNode(t.getName())); 
    } 
    month.add(type); 
    top.add(month); 
    this.getContentPane().setLayout(new BorderLayout()); 
    this.getContentPane().add(new JScrollPane(new JTree(top))); 

    this.pack(); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

} 

public static void main(String a[]) { 
    Test1 t1 = new Test1(); 
} 
}