2012-03-06 1 views
0

Использование DefaultTreeModel, defaultmutbletreenodegetRoot() возвращает его наивысший предок, но как бы вы отправились на одно поколение, чтобы вернуть его второго высшего предка?Java: Получить второго высшего предка потокового treenode

root 
- ancestor 1 
    - some parent 
    - some child 
- ancestor 2 
    - some parent 
    - another parent 
    - some child 

так как найти ancestor 1, учитывая some child в этой отрасли, глубина каждой ветви отличается для каждого ancestor узла под root.

Я урожденная пройти до ancestor 1 от some child, а также для более глубокого отделения, учитывая some child, он найдет ancestor 2.

ответ

0

Попробуйте это:

TreeNode[] nodeArray = tree.getPathToRoot(nodeInQuestion); 
TreeNode secondFromRoot; 

if ((nodeArray != null) && // I'm not sure this can actually happen. 
    (nodeArray.length > 1)) // current node is not the root node. 
{ 
    secondFromRoot = nodeArray[1]; 
} 
else 
{ 
    ... decide what makes sense here. 
} 
Смежные вопросы