I have a JTree with DefaultTreeModel. I need to get to each node of it.
Imagine I have this tree:
[A]
|-[B]
|-[C]
|-[D]
| |-[E]
| |-[F]
| |-[G]
| |-[H]
|-[I]
|-[J]
|-[K]
I need to traverse it and print out:
---[A]---
>[B]
>[C]
>---[D]---
>>---[E]---
>>>[F]
>>>[G]
>>>[H]
>>+++[E]+++
>+++[D]+++
>[I]
>[J]
>[K]
---[A]---
So, I'm using
java.util.Enumeration en = root.preorderEnumeration();
while (en.hasMoreElements()) {}
But I can't come up with a working function. I need to put ---NODE NAME--- when starting a node and end the node with +++NODE NAME+++ and I can't menage to do that. I got it working to a point if only the a Parent node is not 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 it wasn't even working as well as I thought. Here is my current output:
----root (81)----
name
time
displaySize
----New Group1----
BaseX
BaseY
----New Group2----
BaseRadius
----New Group3----
Angle
DistanceFromCenter
++++New Group3++++
PlayerSpeed
MouseX
MouseY
++++New Group3++++
PlayerX
PlayerY
BonusSpawned
actorTags
++++New Group3++++
BonusTime
BonusWhich
+++root+++
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++;
}
I solved it by ditching the Enumeration and building my own function:
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
printNode(root);
public void printNode(DefaultMutableTreeNode node) {
int childCount = node.getChildCount();
System.out.println("---" + node.toString() + "---");
for (int i = 0; i < childCount; i++) {
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
if (childNode.getChildCount() > 0) {
printNode(childNode);
} else {
System.out.println(childNode.toString());
}
}
System.out.println("+++" + node.toString() + "+++");
}
Using recursion you could do something like this psuedocode
Edit My Version of a recursive method
public static void print(DefaultMutableTreeNode aNode)
{
String name = aNode.toString();
int level= aNode.getLevel();
String placement = "";
while (level > 0)
{
placement += ">";
level--;
}
if(aNode.isLeaf())
{
System.out.println(placement + name);
return;
}
System.out.println(placement + "--- " + name + " ---");
for(int i = 0 ; i < aNode.getChildCount() ; i++)
{
print((DefaultMutableTreeNode)aNode.getChildAt(i));
}
System.out.println(placement + "+++ " + name + " +++");
}
This will give you > for the levels also for example my output was:
--- A ---
>--- B ---
>>C
>>--- D ---
>>>E
>>+++ D +++
>+++ B +++
>F
>G
>H
+++ A +++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With