Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.