Home Java Example Java Swing Create JTree using an Object



Create JTree using an Object
Posted on: May 30, 2007 at 12:00 AM
In this section you will learn to create a JTree using object that works with Hashtable.

Create JTree using an Object

     

In this section you will learn  to create a JTree using object that works with Hashtable

Program Description: 

This program simply creates a JTree and uses string type array. Program defines a string type array (Data) to keep multiple data to be displayed in a tree structure. Here the init() method that uses a Hashtable object to store data .This object is added to a  JTree. Data is added in a tree node format. 

Here is the code of this program:  

import javax.swing.*;
import java.awt.*;
import javax.swing.JTree.*;
import java.awt.BorderLayout;
import java.util.*;

public class JTreeObject extends JFrame{
  String[][] Data = {
  {"Amar"}{"BCA""Address","rohini","Delhi"},
  {"Vinod"}{"BCA""Software""Rohini""Delhi"},
  {"Chandan"}{"MCA""Software""Programer""Rohini""Delhi"}
  {"Suman"}{"MCA""Deginer""Web""Delhi"},
  {"Ravi"},{"MCA","Software","programer"}};

  public JTreeObject(){
  super("JTreeobject frame");
  setSize(300250);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void init(){
  Hashtable hash = new Hashtable();
  for (int i = 0; i < Data.length; i+=2){
  hash.put(Data[i][0], Data[i + 1]);
  }
  JTree tree = new JTree(hash);
  getContentPane().add(tree, BorderLayout.CENTER);
  }
  public static void main(String args[]){
  JTreeObject object = new JTreeObject();
  object.init();
  object.setVisible(true);
  }
}

Download this program.

Output of the program:

 

Related Tags for Create JTree using an Object:
cstringarraytabledatahashobjecttreestructmultiplemethodtypehashtabledisplayipstructurethiscreatetabdefineprogramtoiniramjtreestorehtasheitmultiinitspluseulpeiminasmplaytrjispesmeobjprotorstipspdefineseeatplykishamplrayeaandarstrsimusesssriringthstabablhatdispfinipljepleplprndogro


More Tutorials from this section

Ask Questions?    Discuss: Create JTree using an Object   View All Comments

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.