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

    Default need help dynamic tree constuction

    Hi friends,

    i am trying to construct a dynamic tree for continents and countries. it displays the tree but it showing a extra empty branch between the continents and countries node. please help me how to remove it . i am attaching a img file for better understanding of problem.

    here i am posting my code:

    Java Code:
    public class MyTree extends JFrame{
    	
    	Connection con;
    	Statement st;
    	ResultSet rs;
    	List list;
    	JTree tree;
    	
    	public MyTree() throws Exception {
    		// TODO Auto-generated constructor stub
    
    		String sql="select distinct(continent_name) from countries";
    		
    		con=new DBConn().getConnection();
    		
    		st=con.createStatement();
    		rs=st.executeQuery(sql);
    		list=new ArrayList<>();
    		while(rs.next()){
    			list.add(rs.getString(1));			
    		}
    		
    		Object heirarchy[]=list.toArray();
    		
    		
    		DefaultMutableTreeNode root=processHeirarchy(heirarchy);
    		
    		tree=new JTree(root);
    		
    		add(tree);
    		setSize(600, 400);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	}
    	private DefaultMutableTreeNode branchElements(String ele) throws Exception{
    		  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()) {
    
    			  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();
    			  
    			  return processHeirarchy(hierarchy);
    			  
    
    	  }
    
    	private DefaultMutableTreeNode processHeirarchy(Object[] heirarchy) throws Exception {
    		// TODO Auto-generated method stub
    		DefaultMutableTreeNode node=new DefaultMutableTreeNode();
    		DefaultMutableTreeNode child;
    		DefaultMutableTreeNode sub_child;
    		for(int i=0;i<heirarchy.length;i++){
    			child=new DefaultMutableTreeNode(heirarchy[i]);
    			
    			sub_child=branchElements(child.toString());
    			child.add(sub_child);
    			System.out.println(child);
    			node.add(child);
    		}
    		
    		return node;
    	}
    
    	public static void main(String[] args) throws Exception {
    		new MyTree();
    	}
    }
    
    
    DBConn.java
    
    public class DBConn {
    	Connection con;
    	public Connection getConnection() throws Exception{
    		Class.forName("oracle.jdbc.OracleDriver");
    		con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
    		return con;
    	}
    	
    }


    this is the table:

    create table countries(country_name varchar2(20),continent_name varchar2(20));
    insert into countries values('india','asia');
    insert into countries values('china','asia');
    insert into countries values('ameica','south america');
    insert into countries values('england','europe');
    Attached Thumbnails Attached Thumbnails need help dynamic tree constuction-tree.png  

Similar Threads

  1. problem with dynamic tree construction from database
    By akiravelmont in forum New To Java
    Replies: 6
    Last Post: 03-26-2013, 02:42 PM
  2. Display rich:tree in expandable mode when page loads with tree
    By rajpan in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 08-29-2012, 03:33 PM
  3. Dynamic tree in java applet
    By deemu in forum Java Applets
    Replies: 1
    Last Post: 03-20-2012, 05:55 PM
  4. 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, 09:11 AM
  5. dynamic tree creation in jsp
    By rummy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-11-2010, 01:25 PM

Posting Permissions

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