Create JTree using an Object

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.

Create JTree using an Object

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: