Home Java Example Java Swing Traverse JTree Example



Traverse JTree Example
Posted on: May 30, 2007 at 12:00 AM
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.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;

public class JTreeTraverse {
  public static void main(String args[]) {
  JFrame fm = new JFrame("JTree Traverse frame");
  String object[] "Cricket""Hokey""Football" };
  JTree tree = new JTree(object);
  tree.setRootVisible(true);
  TreeModel treemodel = tree.getModel();
  Object TraverseObject = treemodel.getRoot();
  if ((TraverseObject != null&& (TraverseObject instanceof 
   DefaultMutableTreeNode
)) {
  TreeSelectionListener treeListener = new TreeSelectionListener() {
  public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
  JTree tree = (JTreetreeSelectionEvent.getSource();
  TreePath object = tree.getSelectionPath();
  System.out.println(object);
  System.out.println(object.getPath());
  }
  };
  tree.addTreeSelectionListener(treeListener);
  JScrollPane scroll = new JScrollPane(tree);
  fm.getContentPane().add(scroll, BorderLayout.CENTER);
  fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  fm.setSize(200200);
  fm.setVisible(true);
  }
  }
}
 

Download this program.

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

Related Tags for Traverse JTree Example:
javacswingcomclassdatamodeltreestructdisplaystructurecomponentthisnodeoosimplecreaterootframechildwithprogramtoramtreenodejtreepowermodeldeilitaxdesnodesspluseuljavaximinnomodasmntplaytrjispclesemmeprotreemodelwersvaxspdeleeatishampleaandstrsimwingvausesssthswavstchihatdispframpleplprndodeonomogro


More Tutorials from this section

Ask Questions?    Discuss: Traverse JTree Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.