Results 1 to 3 of 3
  1. #1
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default JTree only display .txt

    Ok, so I have a JTree that I use to display files that are on my computer. Is there a way to make it display only .java files and directories? I made it so that it will only display them, but for every other file, it leaves a blank space where the file is.

    here is my getChild()
    Java Code:
    public Object getChild(Object parent, int index) {
             File directory = (File) parent;
             String[] children = directory.list();
             if (((children[index].length() >= 5) && children[index].substring(children[index].length()-4).equals("java")) || (new File(directory, children[index]).isDirectory()))
                {
                return new TreeFile(directory, children[index]);
                }
             return new TreeFile(directory, "NULL");
             }
    I don't know if what I tried was in the wrong direction or what I have to do to make it work.

    I am trying to edit the code from File System Tree : Tree**Swing JFC**Java to make it only display .java files

  2. #2
    macason is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    IMO this is a better example of a tree at play ...

    "java.sun.com/docs/books/tutorial/uiswing/examples/components/TreeDemoProject/src/components/TreeDemo.java"

    Just remove the books and create a recursive function to crawl through your file system from a root node, something like ...

    Java Code:
    public DefaultMutableTreeNode buildtree(String rootOfFileSystem){
        File myFile = new File(rootOfFileSystem);
        File[] myFileList = myFile.list();
    
        for (int i=0;i<myFileList.length;i++){
            if (myFileList[i].isDirecotry){
                create new node 
                this.buildtree(myFileList[i].absolutePath());
            else
               // create new leaf 
               // add to node if it has a .java extention
               // Id be inclinded to use substring by "." to figure out if its .java but  thats just me 
            //then add node to root when its on its last iteration ...
         }
        
    }

    not sure if this will help, but thats my take on it ...sorry i cant post links and the code is pseudo at times, I cant remeber the syntax and I dont have a compiler in front of me .

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    The problem lies in
    Java Code:
    return new TreeFile(directory, "NULL");
    This will indeed create a blank line in the visible tree.

    To solve the problem in the TreeModel, both getChild and getChildCount must be implemented.
    getChildCount needs to preprocess directory.list() and count the number of children to be reported.
    That is, count the number of .java and isDirectory children.
    Then getChild(parent, index) needs to scan through directory.list()
    and return the index'th item which is a .java or directory.

Similar Threads

  1. How to display all TreePath's in JTree?
    By sergey in forum AWT / Swing
    Replies: 2
    Last Post: 11-05-2009, 06:09 AM
  2. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  3. Replies: 1
    Last Post: 06-22-2009, 02:39 AM
  4. How to display a file system in a JTree view
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:43 PM
  5. how to display data in Jtree
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:28 AM

Posting Permissions

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