You are not logged in.

  • Login

1

Monday, March 2nd 2009, 4:20pm

JTree befüllen (nested set)

Hallo,

ich möchte gerne einen JTree populieren, aber irgendwie komme ich nicht weiter.


Ich habe einen Vector mit den Objekten, die im Jtree dargestellt werden sollen. Die Objekte im Vector sind schon nach hierarchie-ebene sortiert. (Ich bin nach dieser Beschreibung vorgegangen). Der Inhalt des Vectors sieht dann so aus:



Source code

1
2
3
4
5
Säugetiere
    * Primaten
          o Halbaffen
          o Affen
    * Nagetiere



Jetzt weiß ich leider nicht, wie ich diese hierarchie am besten in den JTree bekomme.
Dafür gibt es doch bestimmt einen (rekursiven) Algorithmus, oder?
Könnt ihr mir ein paar Tipps oder Links zu Referenzimplementierungen geben? Würde mich sehr darüber freuen.

2

Tuesday, March 3rd 2009, 4:17pm

Habe inzwischen etwas gefunden, das funktioniert. Vieleicht interessiert die Lösung ja auch den ein oder anderen :)
An die Funktion wird übergeben:
ein Vector, in dem sich die Objekte befinden, die den Baum darstellen sollen
Ein File objekt (custom Klasse), welches das Vaterelement darstellt. (Beim ersten Aufruf also das root-element des Baumes).
Ein Int-Wert start, der angibt, ab welchem Punkt der Vector durchlaufen werden soll.
Näheres dazu steht auch nochmal im Kommentar zur Funktion.

Ich hoffe, man kann es so halbwegs verstehen :)
Falls jemandem Unstimmigkeiten auffallen sollten (etwa im comment), dann wäre ich über einen Hinweis dankbar.

Java Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// files --> Vector holding File objects
DefaultMutableTreeNode root = getNode(files, files.get(0), 1);
JTree tree = new JTree(root);
 
/**
	 * This function will accept a Vector from which the tree will be build, a
	 * parent object of type File (package main.File) and an integer start. The
	 * function will search through the Vector, starting at position "start",
	 * looking for File objects that have a higher level than parent. This
	 * means, that they are children of parent. At this point, recursion appears
	 * and the function will be called again for each child, this time using the
	 * child as parent of a new subtree, which will hold all the child's
	 * children. The search stops as soon as the next File object has a
	 * level-value lower than the parent. According to the structure of the
	 * Vector, the current branch ends here. In a "recursion-call", all the File
	 * objects will be traversed again, even those that have been added to the
	 * tree BUT have a lower number than the current parent-node. This would
	 * cause an unwanted return of the function. To counter this, the part of
	 * the Vector that is being searched needs to start at the element that
	 * comes right after the parent element.
	 * 
	 * Sample Vector structure: 
	 * File a - level 0 
	 * File b - level 1 
	 * File c - level 2 
	 * File d - level 2 
	 * File e - level 1 
	 * File f - level 2
	 * 
	 * According tree structure: 
	 * a
	 * . b 
	 * . . c 
	 * . . d 
	 * . e 
	 * . . f
	 * 
	 * 
	 * @param v
	 *            - Vector containing all File objects that are to be inserted
	 *            into the tree.
	 * @param parent
	 *            - parent File.
	 * @param start
	 *            - indicates the position from where to go through the vector.
	 * @return DefaultMutableTreeNode that already contains all children.
	 */
public DefaultMutableTreeNode getNode(Vector<MyFile> v, MyFile parent, int start) {
	DefaultMutableTreeNode node = new DefaultMutableTreeNode(parent);
	if (!parent.isDirectory()) {
		node.setAllowsChildren(false);
	}
 
	// loop through the vector
	for (int i = start; i < v.size(); i++) {
 
		// only consider entries that have a level  1 point higher than the parent (parent level + 1)
		if (v.get(i).getLevel() == parent.getLevel() + 1) {
 
			// recursion - get subtree of the new child and add it to the parent
			node.add(getNode(v, v.get(i), i + 1));
		} else {
 
			// stop looping once an element is encountered that has a lower
			// level than the parent
			if (v.get(i).getLevel() <= parent.getLevel()) {
				break;
			}
		}
	}
	return node;
}

This post has been edited 3 times, last edit by "crest" (Mar 6th 2009, 3:41pm)


3

Tuesday, March 10th 2009, 3:23pm

Schau dir das mal an: java-insel v8 - JTree
Es ist zwar etwas oberflächlich und das TreeModel nicht genug erläutert. Wie man aber Objekte in den JTree einfügt und nutzt ist dort gut ersichtlich und sollte für deine Zwecke vollkommen ausreichen.

Similar threads

Social bookmarks