Results 1 to 7 of 7
  1. #1
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default problem with dynamic tree construction from database

    Hi friends,

    Actually i am trying to construct a tree by retrieving values from database. after selecting a node it event listener is going to call and another query is going to fire and those values should update in the tree. but problem is the query is fired successfully but i am not able to display the records in the frontend. please help me asap.


    <code>

    package com.octopus;

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.event.TreeExpansionEvent;
    import javax.swing.event.TreeExpansionListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;

    public class JTreeStructure extends JFrame implements TreeSelectionListener{
    JTree tree ;
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    DefaultMutableTreeNode root;
    Container content;

    public static void main(String args[]) throws Exception {
    new JTreeStructure();
    }

    public JTreeStructure() throws Exception {

    super("Retrieving data from database ");

    con=new DBConn().getConnection();

    ArrayList list = new ArrayList();
    list.add("Continents");
    try {
    String sql = "Select * from continents";

    st = con.createStatement();
    rs = st.executeQuery(sql);

    while (rs.next()) {

    list.add(rs.getString(1));
    }
    } catch (Exception e) {
    System.out.println(e);
    }
    rs.close();
    st.close();
    con.close();

    Object hierarchy[] = list.toArray();

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    content = getContentPane();

    root = processHierarchy(hierarchy);
    System.out.println(root+"h");
    tree= new JTree(root);


    tree.addTreeSelectionListener(this);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    setSize(700, 400);
    setLocation(300, 100);
    setVisible(true);
    }
    public void valueChanged(TreeSelectionEvent e){
    String ele=e.getPath().getLastPathComponent().toString();
    //System.out.println(ele);
    try {
    DefaultMutableTreeNode root1= branchElements(ele);
    System.out.println(root1);
    JTree tree1=new JTree(root1);
    //System.out.println(tree1);
    this.add(new JScrollPane(tree1),BorderLayout.CENTER);

    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    }

    private DefaultMutableTreeNode branchElements(String ele) throws SQLException{
    con=new DBConn().getConnection();

    ArrayList list = new ArrayList();

    try {
    String sql = "Select country_name from countries where continent_name='"+ele+"'";

    st = con.createStatement();
    rs = st.executeQuery(sql);

    while (rs.next()) {
    //Object value[] = { rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4) };
    list.add(rs.getString(1));

    }
    } catch (Exception e) {
    System.out.println(e);
    }
    rs.close();
    st.close();
    con.close();
    System.out.println(list);
    Object hierarchy[] = list.toArray();
    //
    // DefaultMutableTreeNode root1 = processHierarchy1(hierarchy);
    // System.out.println(root1+" h");
    // // DefaultMutableTreeNode child=new DefaultMutableTreeNode(ele);
    //// JTree tree1
    //// child.add(root);
    return processHierarchy1(hierarchy);


    }

    private DefaultMutableTreeNode processHierarchy1(Object[] hierarchy) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child;
    for (int i = 0 ;i < hierarchy.length; i++) {
    Object nodeSpecifier = hierarchy[i];

    child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
    System.out.println(child);
    node.add(child);
    }
    return (node);
    }


    private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child;
    for (int i = 1 ;i < hierarchy.length; i++) {
    Object nodeSpecifier = hierarchy[i];
    child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
    node.add(child);
    }
    return (node);
    }
    }

    </code>

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: problem with dynamic tree construction from database

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default Re: problem with dynamic tree construction from database

    how come treemodel helps to get rid of this problem

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,804
    Rep Power
    19

    Default Re: problem with dynamic tree construction from database

    Code tags use square brackets here ([code] ... [/code]).

    I would suggest sticking some debugging in there.
    Things like printing out what the actual query used is, the contents of your list after the result set has been processed, that sort of thing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: problem with dynamic tree construction from database

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default Re: problem with dynamic tree construction from database

    these are the table contents in continents table:
    asia
    europe
    south america
    north america

    and these are the contents in my country table:

    country_name continent_name
    india asia
    china asia
    america north america
    england europe
    russai europe


    first it has to populate all the list of continents from DB and then after click on the particular continent it has to show the countries in the DB in tree format. i am able to display the continents but not getting the country values. (* i hope you got the things).

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,804
    Rep Power
    19

    Default Re: problem with dynamic tree construction from database

    And?
    I don't see any debug stuff related to the query causing the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Dynamic tree in java applet
    By deemu in forum Java Applets
    Replies: 1
    Last Post: 03-20-2012, 04:55 PM
  2. Problem--How to create dynamic tree structure in jsp using javascript
    By natu in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-11-2010, 08:11 AM
  3. Need help with a construction of a tree of objects
    By macwadu in forum AWT / Swing
    Replies: 5
    Last Post: 08-09-2010, 10:48 AM
  4. dynamic tree creation in jsp
    By rummy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-11-2010, 12:25 PM
  5. AVL-tree construction
    By student89 in forum Advanced Java
    Replies: 0
    Last Post: 10-27-2008, 05:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •