Results 1 to 3 of 3
Thread: JTree only display .txt
- 12-02-2009, 07:28 AM #1
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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()
I don't know if what I tried was in the wrong direction or what I have to do to make it work.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 am trying to edit the code from File System Tree : Tree*«*Swing JFC*«*Java to make it only display .java files
- 12-03-2009, 10:13 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
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 .
- 12-04-2009, 09:50 PM #3
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
The problem lies in
This will indeed create a blank line in the visible tree.Java Code:return new TreeFile(directory, "NULL");
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
-
How to display all TreePath's in JTree?
By sergey in forum AWT / SwingReplies: 2Last Post: 11-05-2009, 06:09 AM -
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM -
Display tooltip information when a node from JTree is clicked
By javanewbie in forum AWT / SwingReplies: 1Last Post: 06-22-2009, 02:39 AM -
How to display a file system in a JTree view
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:43 PM -
how to display data in Jtree
By paty in forum New To JavaReplies: 1Last Post: 07-24-2007, 12:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks