简体   繁体   中英

TreeView (TreeItem) - get hierarchy index possible?

I have a TreeView that looks like this:

-Parent1
- - Child1
- - Child2
- - - Subchild1
-Parent2
- - Child99

Is there a method or a simple way to get the hierarchical index of a TreeItem in the tree?

For example:

Parent1 would have a hierarchical index of 0 .

Child1 would have a hierarchical index of 1 .

Subchild1 would have a hierarchical index of 2 .

Parent2 would have a hierarchical index of 0 .

Child99 would have a hierarchical index of 1 .

The only thing that gets on mind is using Java Reflect API, getSuperclass() method, as Java doc says: http://da2i.univ-lille1.fr/doc/tutorial-java/reflect/class/getSuperclass.html

Please see the following Example:

public class Subclass{


    public static void main(String[] args) {
        Map map = new HashMap();
        Map map2 = new HashMap();
        printSuperclasses(map);
        printSuperclasses(map2);
}

    static void printSuperclasses(Object o) {
        Class subclass = o.getClass();
        Class superclass = subclass.getSuperclass();
        int i=0;
        while (superclass != null) {
           String className = superclass.getName();
           i++;
           System.out.println(className+" "+i);
           subclass = superclass;
           superclass = subclass.getSuperclass();
        }
     }
}

The output provided by this example is:

java.util.AbstractMap 1

java.lang.Object 2

java.util.AbstractMap 1

java.lang.Object 2

That means that in hierarchy the parent of Map object is AbstractMap(index 1), and the parent of AbstractMap is Object (index 2).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM