Results 1 to 2 of 2
  1. #1
    msambasiva is offline Member
    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0

    Default JTree Lazy loading - Node Expand/Selection issue

    Hi,

    We are able to create JTree using Lazy loading concept. We are facing issue with node selection.
    We are creating Root Level and first level while loading the tree first time. Then if we try to expand any node, we are getting required child info from DB and then creating child nodes of the corresponding node. Following are issue identified with our JTree
    1. Expanded Node1, Able to select child nodes of Node1. If I try to expand Node2, then Node1 gets collapsed.
    2. When I try to expand Node1 and Node2 second time, this time, it's not collapsing Node1.
    i.e when I try to expand any node newly, all other expanded nodes are getting collapsed.

    Following is the code written for our usecase. Please suggest, if Iam doing some thing wrong.
    It would be great help, if you suggest on this issue.

    XML Code:
    package com.oracle.appsfdoc.fusion.sos;
    
    import com.oracle.appsfdoc.fusion.dbh;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Enumeration;
    import javax.swing.JTree;
    import javax.swing.event.TreeExpansionEvent;
    import javax.swing.event.TreeExpansionListener;
    import javax.swing.event.TreeWillExpandListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
    
    public class ProductActivityTaskTree extends DefaultTreeModel implements TreeWillExpandListener,TreeExpansionListener{
        public JTree tree;
        private DefaultMutableTreeNode root;
        private DefaultMutableTreeNode parentA, parentB;
        String dbusername = "FUSDOC";
        String dbpassword = "jeremy";
        String url = "rws60003dbs.us.oracle.com";
        String sidIn = "EPICLNXF";
        Connection connection = null;
        
        Statement stmt = null;
    
        public ProductActivityTaskTree() {
            super(new DefaultMutableTreeNode("ROOT"));
            dbh handle = new dbh();
            try {
                    handle.open(dbusername, dbpassword,
                                    url,sidIn,"true");
                connection = handle.getConnection();
                //handle.close();
            }
            catch( Exception e){
                    System.out.println("[TestDbh.main] Exception caught:\n" + e.getMessage());
            }
            root = (DefaultMutableTreeNode)super.getRoot();
            DefaultTreeModel treeModel = new DefaultTreeModel(root);
            treeModel.addTreeModelListener(new ProductActivityTaskTreeModelListener());
            tree = new JTree(treeModel);
            try {
                createFirstLevel(root);
                //createSecondLevel(root);
                //createThirdLevel(root);
            } catch (SQLException e) {
            }
        }
        
        public void createFirstLevel(DefaultMutableTreeNode root) throws SQLException {
            stmt = connection.createStatement();
            String queryStr = "SELECT PRODUCT_FAMILY_ID, SHORT_NAME, NAME FROM AFC_PRODUCT_FAMILIES ORDER BY SHORT_NAME";
            ResultSet rs = null;
            rs = stmt.executeQuery(queryStr);
            while (rs.next()) {
                DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(rs.getString(2));
                tempNode.add(new DefaultMutableTreeNode(""));
                root.add(tempNode);
            }
        }
    
        public void treeWillExpand(TreeExpansionEvent event) {
            tree =(JTree) event.getSource();
            System.out.println("SOURCE:"+event.getSource());
            System.out.println("PATH"+event.getPath());
            TreePath tPath = event.getPath();
            tree.setExpandsSelectedPaths(true);
            tree.setSelectionPath(event.getPath());
            int pFamilyId =0;
            int pId = 0;
            String pFamilyShortName="";
            String pShortName="";
            String lastPathComponent = tPath.getLastPathComponent().toString();
            if(tPath.getPathCount()==2){
                try{
                    //stmt = connection.createStatement();
                    String queryStr = "SELECT PRODUCT_FAMILY_ID, SHORT_NAME FROM AFC_PRODUCT_FAMILIES WHERE SHORT_NAME = "+"'"+lastPathComponent+"'" + " ORDER BY SHORT_NAME";
                    System.out.println("queryStr"+queryStr);
                    ResultSet rs = null;
                    rs = stmt.executeQuery(queryStr);
                    while(rs.next()){
                        pFamilyId = rs.getInt(1);
                        System.out.println();
                        pFamilyShortName = rs.getString(2);
                        System.out.println("pFamilyId"+pFamilyId);
                    }
                }catch(SQLException exception){
                    System.out.println("Exception:"+exception.getMessage());                        
                    }
                
                createProductNodes(pFamilyShortName,pFamilyId);
            }
            else if(tPath.getPathCount() == 3){
                try{
                    //stmt = connection.createStatement();
                    String queryStr = "SELECT PRODUCT_ID, SHORT_NAME FROM PRODUCTS WHERE SHORT_NAME = "+"'"+lastPathComponent+"'" + " ORDER BY SHORT_NAME";
                    System.out.println("queryStr"+queryStr);
                    ResultSet rs = null;
                    rs = stmt.executeQuery(queryStr);
                    while(rs.next()){
                        pId = rs.getInt(1);
                        System.out.println();
                        pShortName = rs.getString(2);
                        System.out.println("pFamilyId"+pFamilyId);
                    }
                }catch(SQLException exception){
                    System.out.println("Exception:"+exception.getMessage());                        
                    }
                createActivityNodes(pShortName,pId);
            }else{
            }
            System.out.println("LastPathComponent"+tPath.getLastPathComponent().toString());
            System.out.println("Tree Will Expand");
        }
        
        public void createProductNodes(String lastPathComponent,int pFamilyId){
            DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
            Enumeration breadth = root.breadthFirstEnumeration();
            while (breadth.hasMoreElements()) {
                DefaultMutableTreeNode bNode = (DefaultMutableTreeNode)breadth.nextElement();  
                if( bNode.toString().equals(lastPathComponent) && bNode.getChildCount() == 1 ){
                    System.out.println(bNode.toString());
                    try{
                        stmt = connection.createStatement();
                        String queryStr = "select product_id, short_name, name from PRODUCTS  where product_family_id = "+ "'" + pFamilyId +"'" + " order by short_name";
                        System.out.println("queryStr"+queryStr);
                        ResultSet rs = null;
                        rs = stmt.executeQuery(queryStr);
                        while (rs.next()) {
                            String pId = rs.getString(1);
                            String shortName = rs.getString(2);
                            System.out.println("SHORT NAME"+shortName);
                            System.out.println("PID"+pId);
                            DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(shortName);
                            tempNode.setAllowsChildren(true);
                            tempNode.add(new DefaultMutableTreeNode(""));
                            bNode.add(tempNode);
                        }  
                        bNode.remove(0);
                        model.reload();
                    }catch(SQLException exception){
                        System.out.println("Exception:"+exception.getMessage());                        
                        }
                }
                }
            final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
            tree.setCellRenderer(renderer);
            final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
            tree.setCellEditor(editor);
            tree.setEditable(true);
            // listen for changes in the selection
        }
        public void createActivityNodes(String pLastName,int pId){
        DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
        Enumeration breadth = root.breadthFirstEnumeration();
        while (breadth.hasMoreElements()) {
            DefaultMutableTreeNode bNode = (DefaultMutableTreeNode)breadth.nextElement();  
            if(bNode.toString().equals(pLastName) && bNode.getChildCount()==1){
                System.out.println(bNode.toString());
                try{
                    stmt = connection.createStatement();
                    String queryStr = "SELECT ACTIVITY_ID, ACTIVITY_NAME from ACTIVITIES  where PRODUCT_ID = "+ "'" + pId +"'" + " ORDER BY ACTIVITY_NAME";
                    System.out.println("queryStr"+queryStr);
                    ResultSet rs = null;
                    rs = stmt.executeQuery(queryStr);
                    while (rs.next()) {
                        String aId = rs.getString(1);
                        String aShortName = rs.getString(2);
                        System.out.println("A SHORT NAME"+aShortName);
                        System.out.println("AID"+aId);
                        DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(aShortName);
                        tempNode.setAllowsChildren(true);
                        tempNode.add(new DefaultMutableTreeNode(""));
                        bNode.add(tempNode);
                    }  
                    bNode.remove(0);
                    model.reload();
                }catch(SQLException exception){
                    System.out.println("Exception:"+exception.getMessage());                        
                    }
            }
            }
        final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
        tree.setCellRenderer(renderer);
        final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
        tree.setCellEditor(editor);
        tree.setEditable(true);
        // listen for changes in the selection
        }
        
                
        public void treeCollapsed(TreeExpansionEvent event) {
            System.out.println("Tree Collapsed");
        }
        public void treeExpanded(TreeExpansionEvent event) {
            System.out.println("Tree Expanded");
        }
        public void treeWillCollapse(TreeExpansionEvent event) {
            System.out.println("Tree Will Collapse");
        }
    }


    Thanks,
    Samba

  2. #2
    msambasiva is offline Member
    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0

    Default Re: JTree Lazy loading - Node Expand/Selection issue

    Can some one help on our issue?

    Please let me know, if you need more details on our usecase.

    Thanks,
    Samba.

    Quote Originally Posted by msambasiva View Post
    Hi,

    We are able to create JTree using Lazy loading concept. We are facing issue with node selection.
    We are creating Root Level and first level while loading the tree first time. Then if we try to expand any node, we are getting required child info from DB and then creating child nodes of the corresponding node. Following are issue identified with our JTree
    1. Expanded Node1, Able to select child nodes of Node1. If I try to expand Node2, then Node1 gets collapsed.
    2. When I try to expand Node1 and Node2 second time, this time, it's not collapsing Node1.
    i.e when I try to expand any node newly, all other expanded nodes are getting collapsed.

    Following is the code written for our usecase. Please suggest, if Iam doing some thing wrong.
    It would be great help, if you suggest on this issue.

    XML Code:
    package com.oracle.appsfdoc.fusion.sos;
    
    import com.oracle.appsfdoc.fusion.dbh;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Enumeration;
    import javax.swing.JTree;
    import javax.swing.event.TreeExpansionEvent;
    import javax.swing.event.TreeExpansionListener;
    import javax.swing.event.TreeWillExpandListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
    
    public class ProductActivityTaskTree extends DefaultTreeModel implements TreeWillExpandListener,TreeExpansionListener{
        public JTree tree;
        private DefaultMutableTreeNode root;
        private DefaultMutableTreeNode parentA, parentB;
        String dbusername = "FUSDOC";
        String dbpassword = "jeremy";
        String url = "rws60003dbs.us.oracle.com";
        String sidIn = "EPICLNXF";
        Connection connection = null;
        
        Statement stmt = null;
    
        public ProductActivityTaskTree() {
            super(new DefaultMutableTreeNode("ROOT"));
            dbh handle = new dbh();
            try {
                    handle.open(dbusername, dbpassword,
                                    url,sidIn,"true");
                connection = handle.getConnection();
                //handle.close();
            }
            catch( Exception e){
                    System.out.println("[TestDbh.main] Exception caught:\n" + e.getMessage());
            }
            root = (DefaultMutableTreeNode)super.getRoot();
            DefaultTreeModel treeModel = new DefaultTreeModel(root);
            treeModel.addTreeModelListener(new ProductActivityTaskTreeModelListener());
            tree = new JTree(treeModel);
            try {
                createFirstLevel(root);
                //createSecondLevel(root);
                //createThirdLevel(root);
            } catch (SQLException e) {
            }
        }
        
        public void createFirstLevel(DefaultMutableTreeNode root) throws SQLException {
            stmt = connection.createStatement();
            String queryStr = "SELECT PRODUCT_FAMILY_ID, SHORT_NAME, NAME FROM AFC_PRODUCT_FAMILIES ORDER BY SHORT_NAME";
            ResultSet rs = null;
            rs = stmt.executeQuery(queryStr);
            while (rs.next()) {
                DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(rs.getString(2));
                tempNode.add(new DefaultMutableTreeNode(""));
                root.add(tempNode);
            }
        }
    
        public void treeWillExpand(TreeExpansionEvent event) {
            tree =(JTree) event.getSource();
            System.out.println("SOURCE:"+event.getSource());
            System.out.println("PATH"+event.getPath());
            TreePath tPath = event.getPath();
            tree.setExpandsSelectedPaths(true);
            tree.setSelectionPath(event.getPath());
            int pFamilyId =0;
            int pId = 0;
            String pFamilyShortName="";
            String pShortName="";
            String lastPathComponent = tPath.getLastPathComponent().toString();
            if(tPath.getPathCount()==2){
                try{
                    //stmt = connection.createStatement();
                    String queryStr = "SELECT PRODUCT_FAMILY_ID, SHORT_NAME FROM AFC_PRODUCT_FAMILIES WHERE SHORT_NAME = "+"'"+lastPathComponent+"'" + " ORDER BY SHORT_NAME";
                    System.out.println("queryStr"+queryStr);
                    ResultSet rs = null;
                    rs = stmt.executeQuery(queryStr);
                    while(rs.next()){
                        pFamilyId = rs.getInt(1);
                        System.out.println();
                        pFamilyShortName = rs.getString(2);
                        System.out.println("pFamilyId"+pFamilyId);
                    }
                }catch(SQLException exception){
                    System.out.println("Exception:"+exception.getMessage());                        
                    }
                
                createProductNodes(pFamilyShortName,pFamilyId);
            }
            else if(tPath.getPathCount() == 3){
                try{
                    //stmt = connection.createStatement();
                    String queryStr = "SELECT PRODUCT_ID, SHORT_NAME FROM PRODUCTS WHERE SHORT_NAME = "+"'"+lastPathComponent+"'" + " ORDER BY SHORT_NAME";
                    System.out.println("queryStr"+queryStr);
                    ResultSet rs = null;
                    rs = stmt.executeQuery(queryStr);
                    while(rs.next()){
                        pId = rs.getInt(1);
                        System.out.println();
                        pShortName = rs.getString(2);
                        System.out.println("pFamilyId"+pFamilyId);
                    }
                }catch(SQLException exception){
                    System.out.println("Exception:"+exception.getMessage());                        
                    }
                createActivityNodes(pShortName,pId);
            }else{
            }
            System.out.println("LastPathComponent"+tPath.getLastPathComponent().toString());
            System.out.println("Tree Will Expand");
        }
        
        public void createProductNodes(String lastPathComponent,int pFamilyId){
            DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
            Enumeration breadth = root.breadthFirstEnumeration();
            while (breadth.hasMoreElements()) {
                DefaultMutableTreeNode bNode = (DefaultMutableTreeNode)breadth.nextElement();  
                if( bNode.toString().equals(lastPathComponent) && bNode.getChildCount() == 1 ){
                    System.out.println(bNode.toString());
                    try{
                        stmt = connection.createStatement();
                        String queryStr = "select product_id, short_name, name from PRODUCTS  where product_family_id = "+ "'" + pFamilyId +"'" + " order by short_name";
                        System.out.println("queryStr"+queryStr);
                        ResultSet rs = null;
                        rs = stmt.executeQuery(queryStr);
                        while (rs.next()) {
                            String pId = rs.getString(1);
                            String shortName = rs.getString(2);
                            System.out.println("SHORT NAME"+shortName);
                            System.out.println("PID"+pId);
                            DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(shortName);
                            tempNode.setAllowsChildren(true);
                            tempNode.add(new DefaultMutableTreeNode(""));
                            bNode.add(tempNode);
                        }  
                        bNode.remove(0);
                        model.reload();
                    }catch(SQLException exception){
                        System.out.println("Exception:"+exception.getMessage());                        
                        }
                }
                }
            final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
            tree.setCellRenderer(renderer);
            final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
            tree.setCellEditor(editor);
            tree.setEditable(true);
            // listen for changes in the selection
        }
        public void createActivityNodes(String pLastName,int pId){
        DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
        Enumeration breadth = root.breadthFirstEnumeration();
        while (breadth.hasMoreElements()) {
            DefaultMutableTreeNode bNode = (DefaultMutableTreeNode)breadth.nextElement();  
            if(bNode.toString().equals(pLastName) && bNode.getChildCount()==1){
                System.out.println(bNode.toString());
                try{
                    stmt = connection.createStatement();
                    String queryStr = "SELECT ACTIVITY_ID, ACTIVITY_NAME from ACTIVITIES  where PRODUCT_ID = "+ "'" + pId +"'" + " ORDER BY ACTIVITY_NAME";
                    System.out.println("queryStr"+queryStr);
                    ResultSet rs = null;
                    rs = stmt.executeQuery(queryStr);
                    while (rs.next()) {
                        String aId = rs.getString(1);
                        String aShortName = rs.getString(2);
                        System.out.println("A SHORT NAME"+aShortName);
                        System.out.println("AID"+aId);
                        DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(aShortName);
                        tempNode.setAllowsChildren(true);
                        tempNode.add(new DefaultMutableTreeNode(""));
                        bNode.add(tempNode);
                    }  
                    bNode.remove(0);
                    model.reload();
                }catch(SQLException exception){
                    System.out.println("Exception:"+exception.getMessage());                        
                    }
            }
            }
        final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
        tree.setCellRenderer(renderer);
        final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
        tree.setCellEditor(editor);
        tree.setEditable(true);
        // listen for changes in the selection
        }
        
                
        public void treeCollapsed(TreeExpansionEvent event) {
            System.out.println("Tree Collapsed");
        }
        public void treeExpanded(TreeExpansionEvent event) {
            System.out.println("Tree Expanded");
        }
        public void treeWillCollapse(TreeExpansionEvent event) {
            System.out.println("Tree Will Collapse");
        }
    }


    Thanks,
    Samba

Similar Threads

  1. SwiXML - JTree LazyLoad Expand listener issue
    By msambasiva in forum AWT / Swing
    Replies: 5
    Last Post: 06-27-2013, 09:30 AM
  2. lazy loading
    By sid123 in forum Hibernate
    Replies: 2
    Last Post: 05-02-2013, 03:41 PM
  3. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 07:23 AM
  4. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-19-2008, 11: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
  •