views:

122

answers:

3

I'd like to have some kind of file browser like Windows Explorer inside a Java Application.

I just want something that's able to list file inside a folder recursively.

Is there a simple way to do this ?

I already tried to use JFileChooser but it's not what I want.

+3  A: 

This snippet allows you to list all files recursivly. You could use the data to populate a JTree see this tutorial

public class Filewalker { 

    public void walk( String path ) { 

        File root = new File( path ); 
        File[] list = root.listFiles(); 

        for ( File f : list ) { 
            if ( f.isDirectory() ) { 
                walk( f.getAbsolutePath() ); 
                System.err.println( "Dir:" + f.getAbsoluteFile() ); 
            } 
            else { 
                System.err.println( "File:" + f.getAbsoluteFile() ); 
            } 
        } 
    } 

    public static void main(String[] args) { 
        Filewalker fw = new Filewalker(); 
        fw.walk("c:\\" ); 
    } 
} 
stacker
+1 for recursion. You might also like the example in org.netbeans.swing.outline.Outline, mentioned in my answer.
trashgod
Thanks, works like a charm !
Studer
I'm not going to do -1 but: there is a file instance, and then you check if it is a folder. If so, you pass the path and create a new instance, while you can use just the existing one.
Martijn Courteaux
+1  A: 

Perhaps something like this would help you (this is from a quick googling, I don't do GUIs but felt obliged to help):

http://www.java2s.com/Code/Java/Swing-JFC/FileTreewithPopupMenu.htm

whaley
A: 

Empirically, java.awt.FileDialog offers more native look that may suffice. Here's an example that also references a more versatile component, org.netbeans.swing.outline.Outline.

trashgod