Getting Dir structure of computer into a JTree

I need help with reading the full directory structure of a computer [ just mainly identifying the directoreis and placing them into a JTree. I can identify files within a directory. I just need help locating all the directories, subdirectories, and so of the computer. Planning to place the full works into the JTree for browsing.

[344 byte] By [mattcrist] at [2007-9-27 14:42:56]
# 1

Not sure what you're planning to do with a JTree structure of the directory structure but if browsing is all you need then either a simple or some derived version of JFileChooser might do the trick. It can be manipulated in all sorts of ways to filter and restrict the uses.

Just an idea. Hope it helps.

dayoung1 at 2007-7-5 22:42:47 > top of Java-index,Archived Forums,Swing...
# 2

I am getting the full directory structure of a computer [C:\, D:\, etc...] all the files and all the directories and putting them into a JTree. I am currently using JFileChooser, but I do not wish to use this much longer, because I really like the look of the JTree over the JFileChooser

mattcrist at 2007-7-5 22:42:47 > top of Java-index,Archived Forums,Swing...
# 3

The following is a recursive directory search I use to find all the PDFs, starting from a specified directory. The addToFileTable method just puts the result in a HashMap. If you were to track the level of the hierarchy by passing in an int to the method, or perhaps the current node of the tree, you could easily modify this to populate a tree.

protected void scanDirectories( File dir ){

logger.debug("Scanning: " + dir);

File[] files = dir.listFiles();

for( int i = 0; i < files.length; i++ ) {

File file = files[i];

logger.debug( "Current file(" + i + "): " + file );

if( file.isDirectory() ){

// deal with a directory

scanDirectories(file);

}

else{

// deal with a file

String path = file.getAbsolutePath();

if( path.endsWith(".pdf") ){

addToFileTable( "pdf", path );

}

}

}

}

cgarethc at 2007-7-5 22:42:47 > top of Java-index,Archived Forums,Swing...
# 4
The JTreeTable example(s) might be worth a look.Don't have the url off the top of my head, but it's on java.sun.comsomewhere...
john3136 at 2007-7-5 22:42:47 > top of Java-index,Archived Forums,Swing...
# 5
When i was student i wrote JTree programe with for displaying dir structure, if you want then i will send you, not today but tommorrow.if you want then send me mail at : Y_NOT@www.com
Y_NOT at 2007-7-5 22:42:47 > top of Java-index,Archived Forums,Swing...
# 6
If you could, posting on this thread would be very helpful to the people in the future. This will allow others to view it via archive.
mattcrist at 2007-7-5 22:42:47 > top of Java-index,Archived Forums,Swing...
# 7
ok Y_NOT but not today because i am at office right now and code in my home's PC :), i will post tommorrow.
Y_NOT at 2007-7-5 22:42:48 > top of Java-index,Archived Forums,Swing...
# 8

File.listRoots() gives you an array of Files, one for each drive on your system. You can then explore the entries (both directories and files) in each instance of the File array by using list()

You can use this information to create your TreeModel that JTree will query to display information.

sivananda at 2007-7-5 22:42:48 > top of Java-index,Archived Forums,Swing...