0

Using the DefaultTreeModel, defaultmutbletreenode's getRoot() returns it's highest ancestor, but how would you go one generation down to return it's second highest ancestor?

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

so how to find ancestor 1, given some child in this branch, the depth of each branch differs for each ancestor node under root.

I nee to traverse up to ancestor 1 from some child, and also for a deeper branch, given some child, it will find ancestor 2.

KJW
  • 15,035
  • 47
  • 137
  • 243

1 Answers1

0

Try this:

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.
}
DwB
  • 37,124
  • 11
  • 56
  • 82