#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    JTree FTP file Browser


    Hi,
    Im currently writing a program which has some FTP functionality and I need to create an interface for selecting a directory from an FTP server. The complicated bits im managing fine but Ive come across what feels like a really silly problem.

    I've managed to get it to load all the directories and subdirectories into a JTree widget. Directories containing subdirectories are treated as branches and therefore display the correct folder icon. However directories which do not contain subdirectories are treated as leaves and so display a document icon.

    Is there anyway to make a JTree treat all of its nodes as branches, ie. they all display a folder icon whether they are empty or not.

    I realise that a simpler solution to this problem would have been to apply a folder icon to all of the nodes but createImageIcon() is unrecognised even though I have included all the correct libraries.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0
    Are you using Apache Commons? If so, how did you get data to display in a JTree?

    I've not been successful at getting the file structure to load into a JTree.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by Smiz17
    Are you using Apache Commons? If so, how did you get data to display in a JTree?

    I've not been successful at getting the file structure to load into a JTree.
    Hi Smiz17,
    Yes I am using Apache commons.
    The standard way of doing this kind of thing is using a programming technique called recursion, which is basically when you call function inside its own definition so that it loops.
    Presuming you have created an instance of FTPClient called 'ftp' and an instance of JTree as 'tree' and an instance of DefaultMutableTreeNode as 'root':
    Code:
    public String[] getDir(String dir){ //Gets all of the directories from a given directory and returns them as a String Array
     String[] directories;
     try{
      FTPFile[] objects = ftp.listDirectories(dir);
      directories = new String[objects.length]
      for(int i=0; i<objects.length; i++){
       directories[i]=objects[i].getName()
      }
      return directories;
     } catch (IOException ex) {
      return {};
      System.out.println("ERROR");
     }
    }
    
    public void buildtree(String currentdir, DefaultMutableNode model){
     String[] currentcrawl=getDir(currentdir);//Get the current list of directories
     for(String node : currentcrawl){
      DefaultMutableTreeNode currentnode=new DefaultMutableTreeNode(node);//for each directory create a new node
      buildtree(currentdir+"/"+node, currentnode); //build all of the nodes that are a children of the current node
      model.add(currentnode); //add the node to its parent
     }
    }
    buildtree(".", root);
    tree.setModel(new DefaultTreeModel(root));
    This will work fine in a filestructure with a low depth, if you try to run it for anything more complicated it can cause a stack overflow.

    If you are trying to create something with a more complex file structure, you must set it so that it only adds the tree nodes as the user expand elements of the tree which is more complicated.

    Once you have set up the tree nodes you can access your file path with this:
    Code:
    public String seldir=".";
    tree.addTreeSelectionListener(new TreeSelectionListener() {
     public void valueChanged(TreeSelectionEvent arg0){
      Object[] path=arg0.getNewLeadSelectionPath().getPath()
      for(Object i:path){
       seldir+=i.toString()+"/";
      }
     }
    });
    You can then access the current selected directory from seldir.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0
    williammarslett,

    Nice! These methods will work, I will just have to call them after any process where the user may change the filesystem. (Probably not the most efficient, but it will work for now!)

    Lastly, I did see something odd with the Tree Selection code you gave:

    Code:
    public String seldir=".";
    tree.addTreeSelectionListener(new TreeSelectionListener() {
     public void valueChanged(TreeSelectionEvent arg0){
      Object[] path=arg0.getNewLeadSelectionPath().getPath()
      for(Object i:path){
       seldir+=i.toString()+"/";
      }
     }
    });
    I changed the code just a little bit because I was getting wrong directories (it was simply appending each directory I had to the string...simple fix below:

    Code:
    public String seldir=".";
    tree.addTreeSelectionListener(new TreeSelectionListener() {
     public void valueChanged(TreeSelectionEvent arg0){
      String dir = "";
      Object[] path=arg0.getNewLeadSelectionPath().getPath()
      for(Object i : path){
    	if("/".equals(i.toString())){
    		dir += i.toString();
    	}else{
    		dir += i.toString()+"/";
    	}				
        }
      seldir = dir;
     }
    });
    That way the proper path is given, and not just a long string of appended paths.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by williamarslett
    Hi Smiz17,
    Yes I am using Apache commons.
    The standard way of doing this kind of thing is using a programming technique called recursion, which is basically when you call function inside its own definition so that it loops.
    Presuming you have created an instance of FTPClient called 'ftp' and an instance of JTree as 'tree' and an instance of DefaultMutableTreeNode as 'root':
    Code:
    public String[] getDir(String dir){ //Gets all of the directories from a given directory and returns them as a String Array
     String[] directories;
     try{
      FTPFile[] objects = ftp.listDirectories(dir);
      directories = new String[objects.length]
      for(int i=0; i<objects.length; i++){
       directories[i]=objects[i].getName()
      }
      return directories;
     } catch (IOException ex) {
      return {};
      System.out.println("ERROR");
     }
    }
    
    public void buildtree(String currentdir, DefaultMutableNode model){
     String[] currentcrawl=getDir(currentdir);//Get the current list of directories
     for(String node : currentcrawl){
      DefaultMutableTreeNode currentnode=new DefaultMutableTreeNode(node);//for each directory create a new node
      buildtree(currentdir+"/"+node, currentnode); //build all of the nodes that are a children of the current node
      model.add(currentnode); //add the node to its parent
     }
    }
    buildtree(".", root);
    tree.setModel(new DefaultTreeModel(root));
    This will work fine in a filestructure with a low depth, if you try to run it for anything more complicated it can cause a stack overflow.

    If you are trying to create something with a more complex file structure, you must set it so that it only adds the tree nodes as the user expand elements of the tree which is more complicated.

    Once you have set up the tree nodes you can access your file path with this:
    Code:
    public String seldir=".";
    tree.addTreeSelectionListener(new TreeSelectionListener() {
     public void valueChanged(TreeSelectionEvent arg0){
      Object[] path=arg0.getNewLeadSelectionPath().getPath()
      for(Object i:path){
       seldir+=i.toString()+"/";
      }
     }
    });
    You can then access the current selected directory from seldir.

    I am kind of new to all these.....sorry am coming here so late........i have tried to implement this code and it just wont work for me.........can you please please show me the whole code and how its implemented......

IMN logo majestic logo threadwatch logo seochat tools logo