简体   繁体   English

TreeView(TreeItem)-可能获取层次结构索引吗?

[英]TreeView (TreeItem) - get hierarchy index possible?

I have a TreeView that looks like this: 我有一个TreeView看起来像这样:

-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? 是否有一种方法或一种简单的方法来获取树中TreeItem的层次结构索引?

For example: 例如:

Parent1 would have a hierarchical index of 0 . Parent1的层次结构索引为0

Child1 would have a hierarchical index of 1 . Child1的层次结构索引为1

Subchild1 would have a hierarchical index of 2 . Subchild1的层级索引为2

Parent2 would have a hierarchical index of 0 . Parent2的层次结构索引为0

Child99 would have a hierarchical index of 1 . Child99的层次结构索引为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 唯一想到的是使用Java Reflect API,getSuperclass()方法,正如Java文档所说的那样: 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.util.AbstractMap 1

java.lang.Object 2 java.lang.Object 2

java.util.AbstractMap 1 java.util.AbstractMap 1

java.lang.Object 2 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). 这意味着在层次结构中,Map对象的父对象是AbstractMap(索引1),而AbstractMap的父对象是Object(索引2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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