2017-01-27 2 views
-1

Ich habe diese Tabelle:Wie erstellt man eine JTree-Struktur aus Array-Daten?

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

Ich möchte seine Aufzeichnungen auf einem JTree auf diese Weise zeigen:

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

Wie kann ich das tun?

Ich habe eine Menge Dokumentation darüber gefunden, aber ich kann immer noch nicht verstehen, wie man das von einem Array unbekannter Länge macht.

Ich frage das, weil mit UCanAccess ich alle Datensätze auswählen und alle in einem Array speichern.

Was könnte der effizienteste Weg sein, diese Knoten zur Struktur hinzuzufügen? (bedenken Sie, dass ich 300+ Elemente speichern muss)

+0

UCanAccess bietet eine JDBC-Schnittstelle. Es speichert keine Datensätze im Array! – jamadei

+0

@jamadei bearbeitet! Kennst du die Antwort? – valbuxvb

+0

* "Ich kann immer noch nicht verstehen, wie man das von einem Array unbekannter Länge macht" * - Sie wissen, dass Sie [durch die Elemente in einem Array iterieren können] (http://stackoverflow.com/q/2331509/2144390) , Recht? –

Antwort

1

Schleifen Sie das Array und fügen Sie einen Baumknoten hinzu, wenn sich der Typ oder der Monat ändert. Dies ist ein sehr vereinfachtes Beispiel. Es verwendet eine Klasse Test2.java, die das Array beschreibt. Stellen Sie sicher, Ihre Daten kommt in der richtigen Art und Weise sortierte:

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; 
} 

} 

Und diese Klasse zeigt eine JTree in einem JFrame:

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(); 
} 
} 
Verwandte Themen