In this section, you will read about traversal of a tree and its node .Teaches, displaying the node and its path on the command prompt. The Javax.swing.JTree class is a powerful swing component to display data in a tree structure.
This program creates a tree with root and child nodes on the frame. The TreeModel is a simple tree data model that uses TreeNodes. The TreeSelectionListener class performs an action when you click the node of tree. This uses getSelectionPath() method for getting the first path in the selection of node in a tree. Finally, this program performs the selection of a node of the tree to be displayed like the name of tree and its path.
This is the method that returns the first path of the selected tree.
Here is the code of this program:
Output of this program:
After click tree node the command prompt will dispaly:
Posted on: May 30, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles