Moving all nodes of a layered JTree

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:

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

}
+4
source share
3 answers

, - psuedocode

  • root
  •   - node
  • --- node name ----
  • node   - ( 2)
  • +++ node ++++

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 + " +++");
}

> , , :

--- A ---
>--- B ---
>>C
>>--- D ---
>>>E
>>+++ D +++
>+++ B +++
>F
>G
>H
+++ A +++
+2

, :

    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() + "+++");

}
+2

- " " / " ". , .

( , ), , node. ( , ), .

-2

All Articles