isRoot() in DefaultMutableTreeNode

239 views Asked by At

Why there is an isRoot() function in DefaultMutableTreeNode? I ask because I have a Class that extends DefaultMutableTreeNode. I would like to be able to use a single instance of this class in multiple different JTree objects. However, it would seem that isRoot() is a question I should be asking the tree object itself. Can anyone provide any insight as to why this function exists at a node level instead of the tree level?

1

There are 1 answers

0
Maura On

The tree object can't be the root of the tree; the JTree object isn't a node.

DefaultMutableTreeNode is designed to facilitate recursive traversal of the tree. Since a recursive method processing the tree will only have access to one node (and its subsequent nodes) at a time, the only way for the method to distinguish the root of the tree is for the nodes to inidivdually keep track of whether or not they are the root of the tree.