This discussion is archived
2 Replies Latest reply: Feb 18, 2013 5:40 AM by 803453 RSS

JTree doesn't display expanded node

803453 Newbie
Currently Being Moderated
Hi,
I've written a JTrre extension to navigate though file-system that load node only if necessary.
That way it is really fast to load, and nothing get loaded if unnecessary.
It is like a JFileChooser;
tecnically my code is just an implementatio of TreeWillExpandListener and TreeSelectionListener that load the content of the selected node, if not already loaded (it doesn't "see" FileSystem change that way, but i don't care, and BTW it is easy to remove the "not load if loaded" check and add a "clear node content")
Every folder is a DefaultMutableTreeNode with
@Override
public boolean isLeaf(){
     return false;
}
everything works, i can navigate though tree flawlessly if starting from root node, the problem started when trying to load a saved path.

because my code load dynamically the tree, for every peace of path i just call expandNode(partialPath), i can see that node are loaded, BUT they aren't visible in the JTree swing, only the root node!.
Calling the JTree.isVisible(fullPathWithoutLeaf) return true....
Also if you double click the root node, nothing happen as like it is collapsing the subnode, then double click again and here there are the sub-node! Because they aren't loaded again, that means they was correctly loaded at the beginning!!

What i'm missing?

i've also tested (afrer calling expand on partial path):

setExpandsSelectedPaths(true);
setSelectionPath(fullPathWithoutLeaf);
makeVisible(fullPathWithoutLeaf);
expandPath(fullPathWithoutLeaf);

code:
JTree+Listener code: codepad.org/pLMrVlBd
FakeEmptyTreeNode: codepad.org/ecrfDZNV

Edited by: lesto on Feb 14, 2013 4:55 AM

Edited by: lesto on Feb 14, 2013 4:58 AM
  • 1. Re: JTree doesn't display expanded node
    StanislavL Pro
    Currently Being Moderated
    Looks like problem of proper defining of path. To expand path tree starts from root and for current node looks for child which equals the next path element. When you store path and load the path's TreeNode may not have equal tree node.

    Try to do the same kind of manyally. For root of the tree search through all children and compare their user objects with user object stored in the next path item. Expand when it is found. Do the same for the found node etc.
  • 2. Re: JTree doesn't display expanded node
    803453 Newbie
    Currently Being Moderated
    thanks, you response pointed me in the right direction, i was doing
    model.insertNodeInto(node, tmp, tmp.getChildCount());
    where tmp was a Node extrapolated from treePath, treePath wasn't from the Tree in case of automatic load, but a temporary object.

    This mean data was attached to my temporary treePath instead of JTree treePath

    Now with:
    tmp = JTree.getModel().getRoot();
    for (int i=1; i < treePath.getPathCount(); i++){
         tmp = getTreePath( model, tmp, treePath.getPathComponent(i).toString() );
    }
    i can load sucessully the JTree node.

    BUT

    now only root node get expanded...

    full code with example: (i know it is a little spaghetti, but because i was trying to track the error in any possible way)
    package Principale;
    import java.io.File;
    import java.util.EventObject;
    
    import javax.swing.JFrame;
    import javax.swing.JTree;
    import javax.swing.event.TreeExpansionEvent;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeWillExpandListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.ExpandVetoException;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;
    
    
    public class JTreeFile extends JTree implements TreeWillExpandListener, TreeSelectionListener{
         
         /**
          * 
          */
         private static final long serialVersionUID = 1L;
    
         public JTreeFile(String dir){
              super (new FakeEmptyTreeNode( dir ) );
              
              this.addTreeWillExpandListener(this);
              //this.addTreeSelectionListener(this);
         }
    
         public static DefaultMutableTreeNode createNode(File dir){
              return new DefaultMutableTreeNode(dir.getName());
         }
         
         @Override
         public void treeWillCollapse(TreeExpansionEvent arg0) throws ExpandVetoException {
              // TODO Auto-generated method stub
              
         }
    
         @Override
         public void treeWillExpand(TreeExpansionEvent arg0) throws ExpandVetoException {
              loadSubNode(arg0, arg0.getPath());
              
         }
    
         @Override
         public void valueChanged(TreeSelectionEvent arg0) {
              loadSubNode(arg0, arg0.getPath());
         }
         
         private void loadSubNode(EventObject arg0, TreePath treePath){
              loadSubNode(treePath);
         }
         
         private void loadSubNode(TreePath treePath){
              DefaultTreeModel model = (DefaultTreeModel)this.getModel();
              DefaultMutableTreeNode tmp= (DefaultMutableTreeNode)model.getRoot();
              System.out.println("tmp begin: "+tmp);
              System.out.println( "changed path: "+treePath.getPath().toString() );
              
              String path="";
              for (int i=0; i < treePath.getPathCount(); i++){
                   if (i>0){
                        tmp = getTreePath( model, tmp, treePath.getPathComponent(i).toString() );
                        
                        System.out.println("tmp: "+tmp);
                        if (path.charAt(path.length()-1)!=File.separatorChar){
                             path += File.separatorChar;
                        }
                   }
                   
                   path += treePath.getPathComponent(i);
                   
                   System.out.println("path aggiungo:"+treePath.getPathComponent(i).toString());
                   
                   
                   //System.out.println("path trovato: "+treePath.getPathComponent(i).toString());
                   //
              }
              System.out.println( "changed elaborated path: "+path );
    
              
              //DefaultMutableTreeNode tmp = (DefaultMutableTreeNode)arg0.;
              
              //DefaultMutableTreeNode tmp= (DefaultMutableTreeNode)treePath.getLastPathComponent();
              
              System.out.println( "Node: "+tmp+" leaf: "+tmp.isLeaf()+" child: "+tmp.getChildCount() );
              if (!tmp.isLeaf() && tmp.getChildCount() == 0){
                   File f = new File(path);
                   if (f.listFiles()==null){
                        System.out.println( "file: "+f.getAbsolutePath()+" nHas null sub.. permission denied? wrong path?");
                        return;
                   }
                   System.out.println( "file: "+f.getAbsolutePath()+" number of subelement "+f.listFiles().length );
                   for (File sub : f.listFiles()){
                        DefaultMutableTreeNode node;
                        if ( sub.isDirectory() ){
                             node = new FakeEmptyTreeNode(sub.getName());
                             //System.out.println("aggiungo dir: "+sub.getName() );
                        }else{
                             node = new DefaultMutableTreeNode(sub.getName());
                             //System.out.println("aggiungo file: "+sub.getName() );
                        }
                        //tmp.add(node);
                        model.insertNodeInto(node, tmp, tmp.getChildCount());
                        //model.reload();
                   }
                   
              }
              
              //System.out.println("path parziale: "+getTreeText(model, model.getRoot(), "")+" fine");
              //
              //expandPath(treePath);
         }
    
         public void changeFileSystem(String selectedItem) {
              this.setModel(new DefaultTreeModel(new FakeEmptyTreeNode( selectedItem )));
         }
    
         public void setURI(String path) {
              String regex = "\\Q"+File.separatorChar+"\\E";
              String split[] = path.split(regex);
              
              System.out.println("path: "+path);
              if (split == null || split.length <1)
                   return;
    
              
              split[0] = split[0]+"\\";
              System.out.println("Set FileSystem: "+split[0]);
              
              changeFileSystem(split[0]);
              
              setRootVisible(true);
              
              TreePath tmp = new TreePath( new FakeEmptyTreeNode(split[0]) );
              //this.setExpandsSelectedPaths(true);
              
              this.expandPath(tmp);
              
              for (int i=1;i<split.length-1;i++){ //the last part of path IS a file, don't set it as default
                   if (split.equals(""))
                        continue;//don't add void children
                   
                   System.out.println("\n\npoi: "+split[i]);
                   
                   tmp = tmp.pathByAddingChild(new FakeEmptyTreeNode(split[i]));
                   
                   //this.setSelectionPath(tmp);
                   
                   this.expandPath(tmp);
                   //fireTreeExpanded(tmp);
                   
                   
              }
              
              //((DefaultTreeModel)this.getModel()).reload();
              /*
              this.setSelectionPath(tmp);
              
              this.expandPath(tmp);
              */
              //this.makeVisible(tmp);
              
              //System.out.println("visibile: "+ isVisible(tmp));
              
              //this.repaint();
              
              DefaultTreeModel model = (DefaultTreeModel)this.getModel();
              //FakeEmptyTreeNode root = (FakeEmptyTreeNode)model.getRoot();
              
              System.out.println("path finale:\n"+getTreeText(model, model.getRoot(), ""));
              
              
              path="";
              for (int i=0; i < tmp.getPathCount(); i++){
                   if (!path.equals("")){
                        path += '-';//File.separator;
                   }
                   path += tmp.getPathComponent(i);
                   System.out.println("path expand:"+tmp.getPathComponent(i));
              }
              System.out.println( "changed expand path: "+path );
              this.makeVisible(tmp);
              this.expandPath(tmp);
              this.setSelectionPath(tmp);
         }
         
         private static String getTreeText(TreeModel model, Object object, String indent) {
         String myRow = indent + object + "\n";
         for (int i = 0; i < model.getChildCount(object); i++) {
         myRow += getTreeText(model, model.getChild(object, i), indent + " ");
         }
         return myRow;
         }
         
         private static DefaultMutableTreeNode getTreePath(TreeModel model, DefaultMutableTreeNode object, String cosa) {
         for (int i = 0; i < model.getChildCount(object); i++) {
              //System.out.println("confronto: "+model.getChild(object, i)+" con "+cosa);
              if ( model.getChild(object, i).toString().equals(cosa) ){
                   return (DefaultMutableTreeNode)model.getChild(object, i);
              }
         }
              return null;
         }

         public static void main(String args[]){
              
              JFrame test1 = new JFrame();
              JTreeFile fileManager1 = new JTreeFile("C:\\");
              test1.add(fileManager1);
              test1.setTitle("test1");
              fileManager1.setURI("C:\\Program Files");
              test1.setSize(200, 500);
              test1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test1.setVisible(true);
              
              JFrame test2 = new JFrame();
              JTreeFile fileManager2 = new JTreeFile("C:\\");
              test2.add(fileManager2);
              fileManager2.setURI("C:\\");
              test2.setTitle("test2");
              test2.setSize(200, 500);
              test2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test2.setVisible(true);
              
              
              JFrame test3 = new JFrame();
              JTreeFile fileManager3 = new JTreeFile("C:\\");
              test3.add(fileManager3);
              test3.setTitle("test3");
              //fileManager3.setURI("C:\\");
              test3.setSize(200, 500);
              test3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test3.setVisible(true);
         }
    }
    package Principale;
    import javax.swing.tree.DefaultMutableTreeNode;


    public class FakeEmptyTreeNode extends DefaultMutableTreeNode {

         /**
         *
         */
         private static final long serialVersionUID = 1L;

         public FakeEmptyTreeNode(String string) {
              super(string);
         }

         @Override
         public boolean isLeaf(){
              return false;
         }
    }
    Edited by: lesto on Feb 18, 2013 5:37 AM
    
    Edited by: lesto on Feb 18, 2013 5:38 AM
    
    Edited by: lesto on Feb 18, 2013 5:39 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points