【问题标题】:TreeView (TreeItem) - get hierarchy index possible?TreeView (TreeItem) - 获取层次索引可能吗?
【发布时间】:2014-11-25 21:56:56
【问题描述】:

我有一个看起来像这样的 TreeView:

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

有没有一种方法或简单的方法来获取树中TreeItem 的层次索引?

例如:

Parent1 会有一个分层的索引为 0

Child1 的层次结构索引为 1

Subchild1 会有一个分层的索引为 2

Parent2 会有一个分层的索引为 0

Child99 的层次结构索引为 1

【问题讨论】:

    标签: java javafx treeview


    【解决方案1】:

    唯一想到的是使用 Java Reflect API,getSuperclass() 方法,正如 Java 文档所说:http://da2i.univ-lille1.fr/doc/tutorial-java/reflect/class/getSuperclass.html

    请看下面的例子:

    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();
            }
         }
    }
    

    这个例子提供的输出是:

    java.util.AbstractMap 1

    java.lang.Object 2

    java.util.AbstractMap 1

    java.lang.Object 2

    也就是说在hierarchy中Map对象的父对象是AbstractMap(索引1),AbstractMap的父对象是Object(索引2)。

    【讨论】:

    • 感谢您的回答,但这与 TreeView 无关。这是关于类和继承的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 2015-07-03
    相关资源
    最近更新 更多