I have a JTree with DefaultTreeModel. I need to get to each node.
Imagine that I have this tree:
[A]
|-[B]
|-[C]
|-[D]
| |-[E]
| |-[F]
| |-[G]
| |-[H]
|-[I]
|-[J]
|-[K]
I need to go through it and print:
---[A]---
>[B]
>[C]
>---[D]---
>>---[E]---
>>>[F]
>>>[G]
>>>[H]
>>+++[E]+++
>+++[D]+++
>[I]
>[J]
>[K]
---[A]---
So i use
java.util.Enumeration en = root.preorderEnumeration();
while (en.hasMoreElements()) {}
But I can not come up with a working function. I need to put --- NODE NAME --- when starting node and terminate node c +++ NODE NAME +++, and I cannot do this. I earned it unless the parent node is the last element of another parent. But it breaks when the last node is also a parent. Any help would be appreciated.
Edit:
And now I noticed that it didn’t even work as good as I thought. Here is my current output:
name
time
displaySize
BaseX
BaseY
BaseRadius
Angle
DistanceFromCenter
PlayerSpeed
MouseX
MouseY
PlayerX
PlayerY
BonusSpawned
actorTags
BonusTime
BonusWhich
Edit2:
while (en.hasMoreElements()) {
nodeTemp = node;
node = (DefaultMutableTreeNode) en.nextElement();
String nodeName = node.toString();
if (node.getChildCount() > 0) {
System.out.println("---" + nodeName + "---");
} else {
if (nodeTemp.getChildCount() == 0 && nodeTemp.getParent() != node.getParent()) {
System.out.println("+++" + nodeName + "+++");
loopCount++;
}
System.out.println(nodeName);
}
loopCount++;
}
source
share