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


 
  
 
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
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Creating a JTree Component

                         

In this section, you will learn about the JTree and its components as well as how to create an JTree component. Here, first of all we are going to describe about the JTree and its component. The java.swing  package provides the JTree and its component. 

JTree: The tree is a special type of graph that designed for displaying data with the hierarchical properties by adding nodes to nodes and keeps the concept of parent and child node.

Node: A node is an object at any position within the JTree where the data are associated or being represented. 

Path: The path is the collection of contiguous set of nodes that contains one or many nodes. The path is empty or null when the path has not any node.

Leaf: This is a special types of node at the end of a path. The leaf node has not connected to more nodes.

Root: This is the node of highest point within the hierarchy in tree. 

Parent:  It represents the relationship of node with another node. In object-oriented programming concepts the parent is a super class.

Child: It represents the relationship of node with another node. In object-oriented programming concepts the child is a subclass of its parent. It inherits all properties of its parent.

Description of program:

The following program creates the simple JTree component. The tree is a special type of graph that build the tree hierarchy by adding nodes to nodes and keeps the concept of parent and child node or root and a child of root node. Whenever you will go to create a tree, you will need root node that will be created by the DefaultMutableTreeNode() method. All objects of DefaultMutableTreeNode are added in the specifying parent or child nodes that will be specified by you and root node are added in the JTree object that will also added on the frame.

Description of code:

DefaultMutableTreeNode("Color", true):
This is the constructor of DefaultMutableTreeNode class. It creates a tree node with no root node, the child of root node, specified by user object and it allows only children that have to be specified. It takes boolean types values either 'true' or 'false'. If you will take 'true' that means children node are allowed otherwise  it will display the IllegalStateException

JTree(TreeNode parent):
This is the constructor of JTree class that shows a set of hierarchical data. It returns a JTree with given TreeNode that displays the root node in a tree. By default, the tree defines the leaf  node of a tree. It takes the following types of arguments:

        parent:  This is the name of an object of TreeNode that displays the root node. 

Here is the code of program:

import javax.swing.*;
import javax.swing.tree.*;

public class TreeComponent{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Creating a JTree Component!");
    DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Color"true);
    DefaultMutableTreeNode black = new DefaultMutableTreeNode("Black");
    DefaultMutableTreeNode blue = new DefaultMutableTreeNode("Blue");
    DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("Navy Blue");
    DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("Dark Blue");
    DefaultMutableTreeNode green = new DefaultMutableTreeNode("Green");
    DefaultMutableTreeNode white = new DefaultMutableTreeNode("White");
    parent.add(black);
    parent.add(blue);
    blue.add(nBlue);
    blue.add(dBlue);
    parent.add(green );
    parent.add(white);
    JTree tree = new JTree(parent);
    frame.add(tree);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}

Download this example.

Output of program:

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
create txt file in spe
Photoshop Cut out imag
Java Count words,reve
hibernate many to many
jsp bank examples
log4j:WARN No appender
file read and write i
value change listener
Add listener to compon
xml to Excel using ja
Photoshop Text Effects
source code for displa
comparing the elements
java quiz
how to get applicate s
Creating Website with
navigate to other page
day number of year
seam tutorial
Removing
tuxedo
prefix=
"day number of year"
java servlets
source code for displa
load file
array element in php
how can create panel g
maven
GMT
Setting Header
Nested WHILE LOOPS
Photoshop Digital Art
Error handling in stru
program
websphere administrati
dojo dialog
program in java to dis
substring
convert date to number
struts2 file upload
compression of a file
How to show expression
Flash Special Effects
Photoshop Photo Effect
Database MS Access Dat
Tutorial:FlashBasicPas
insert data to text fi
passing value from ser
adding scrollbar in Jp
jsp Insert data in mys
Photoshop Textures and
Selection Sort In Java
Photoshop Digital Art
Exercise - Pad Left
Set hashCode
Tomcat deploy JSP
How to create Jasperpr
transientinjava
netbeans jsp
Catching Exception usi
convert Time to String
count the number of di
Determining If a Year
netbeans jsp jdbc
jsp bean get property
array
Photoshop Textures and
delete data to text fi
how to disable the tex
jta
split
covert string to ident
passing values from js
getting xml sub elemen
addition of two number
customise css in jsp
JScrollPane
Struts HTML EL tags
covert uppercase to lo
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

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.