-1

I have a JTree which contains file/directory, i want to get a list which contains current list of this JTree.

How can i do it?

menuItemZip.addActionListener(new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            boolean exist=false;
            File[] files = (File[]) tree.getModel().getRoot();
            for (File file : files) {
                if (file.getName().equals(selectedFile.getName()+".zip"))
                    exist = true;
                break;
            }
            if(exist ==true)
            new ZipWorkers(selectedFile,WORKING,status).execute();
            btnRefresh.doClick();
        }
    });

Edited as new solution: just i don't know if it is a good solution?

menuItemZip.addActionListener(new AbstractAction() {
        public void actionPerformed(ActionEvent e) {     
           Enumeration enumeration =  ((TreeNode) tree.getModel().getRoot()).children();
            String filename = selectedFile.getName()+".zip";
            boolean exist=false;
            while (enumeration.hasMoreElements()){
                String file = ((File) enumeration.nextElement()).getName();
                   if(file.equals(filename)){
                       exist=true;
                       break;
                   }
            }
         if(exist ==true)
            new ZipWorkers(selectedFile,WORKING,status).execute();
            btnRefresh.doClick();
        }
    });
itro
  • 7,006
  • 27
  • 78
  • 121
  • 1
    This question is confusing. A tree contains a descending hierarchy of items - in this case files. Can you clarify what your list is? – ControlAltDel Jun 22 '12 at 13:55
  • 2
    See [File Browser GUI](http://codereview.stackexchange.com/questions/4446/file-browser-gui). .. I might have completely misunderstood what you are asking. Can you clarify? – Andrew Thompson Jun 22 '12 at 13:55
  • My list is a list of Files. I think if you look at the code you can see the `File[]` files. – itro Jun 22 '12 at 14:39

3 Answers3

3

You will need to recursively descend into directories (see File.isDirectory() to determine if it is). The recursiveness can be achieved by writing a function that iterates over an array of files and will call itself with the children of a directory

boolean doesExist(File[] files, String searchFileName) {
  boolean exists = false;
  for (File f : files) {
    if (f.getName().equals(searchFileName)) {
      exist = true;
    } else if (f.isDirectory()) {
      exist = doesExist(f.listFiles(), searchFileName);
    }
    if (exist) {
      break; // no need to proceed further
    }
  }
  return exist;
}

then call it with

doesExist((File[]) tree.getModel().getRoot(), selectedFile.getName()+".zip");
Attila
  • 28,265
  • 3
  • 46
  • 55
  • I get `Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException:` I think there is problem with this : `(File[]) tree.getModel().getRoot()` – itro Jun 22 '12 at 15:24
  • @itro - I assumed it worked that way for you already based on the code you posted. See mKorbel's solution using `TreeNode` instead of `File[]` -- same idea of using the recursion, though – Attila Jun 22 '12 at 15:28
  • I did it as below and work but i'm not sure if it is good way? ` Enumeration enumeration = ((TreeNode) tree.getModel().getRoot()).children(); String filename = selectedFile.getName()+".zip"; boolean exist=false; while (enumeration.hasMoreElements()){ String file = ((File) enumeration.nextElement()).getName(); if(file.equals(filename)){ exist=true; break; } }` – itro Jun 22 '12 at 16:14
  • @itro - you seem to have some type inconsistencies: `enumeration` should contain `TreeNode`s (`TreeNode.children()` returns an enumeration of `TreeNode`s -- see `TreeNode.getChildAt()`), yet you cast the elements to `File`. – Attila Jul 02 '12 at 15:25
3

easiest of ways is, nice examples here and here

import java.util.Enumeration;   
import javax.swing.JTree;
import javax.swing.tree.TreeNode;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTree tree = new JTree();
    visitAllNodes(tree);
  }
  public static void visitAllNodes(JTree tree) {
    TreeNode root = (TreeNode) tree.getModel().getRoot();
    visitAllNodes(root);
  }

  public static void visitAllNodes(TreeNode node) {
    System.out.println(node);
    if (node.getChildCount() >= 0) {
      for (Enumeration e = node.children(); e.hasMoreElements();) {
        TreeNode n = (TreeNode) e.nextElement();
        visitAllNodes(n);
      }
    }
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Also consider a variation of FileTreeModel, discussed here. The advantage is that you need not examine nodes until required.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045