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.
Traverse JTree Example
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.
Program Description:
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.
getSelectionPath():
This is the method that returns the first path of
the selected tree.
Here is the code of this program:
import javax.swing.*;
|
Output of this program:
After click tree node the command prompt will dispaly:
C:\javaswing>java JTreeTraverse [root, Hokey] [Ljava.lang.Object;@5afd29 [root] [Ljava.lang.Object;@b09e89 [root, Hokey] [Ljava.lang.Object;@1bf6770 |