-1

I'd like to show in my Java application all files inside of a folder, like the windows explorer do.

Windows explorer

I'd like to create a GUI like this: enter image description here

There you can see, that all folders and files of a path are listed.

Does anyone has a good solution for that?

jollepe
  • 71
  • 13
  • 1
    See also the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784) which uses a tree & table to show files. For something that looks more like above, swap the table for a `JList`. – Andrew Thompson Sep 29 '16 at 08:29
  • *"good solution"* As an aside: What exactly do you mean by 'good solution'? Do you need to know what components to use? Do you need to know how to implement a 'back' button? Please be specific, as *'How would I code that GUI?'* is far too broad for SO. – Andrew Thompson Sep 29 '16 at 08:34
  • You can use a tree or FileChooser from Java. But if you want something like the windows explorer, you may have to create it yourself (which is definitely doable). First you must know how to read the entire list of files from a specific path first, then just display them in icon (where you can create your own icon class) – user3437460 Sep 30 '16 at 22:03

3 Answers3

0

Try listing the files usibng the method from the file class:

Example:

    final File[] x = new File("C:\\").listFiles();
    for (final File file : x) {
        System.err.println(file.getName());
        System.err.println(file.isDirectory());
        System.err.println(file.isFile());
    }
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Not sure how exactly you want to show them but here is one way:

public void listFilesForFolder(final File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } else {
            System.out.println(fileEntry.getName());
        }
    }
}

final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);

Props to: Read all files in a folder

In case you are building a GUI I would suggest using a File Chooser: https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Community
  • 1
  • 1
PlsWork
  • 1,958
  • 1
  • 19
  • 31
0

Try ApacheIO for this. With minimum code you can achieve it.

 Class: FileUtils
 Method:
        iterateFiles(File directory, String[] extensions, boolean recursive)
         Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

        iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
         Allows iteration over the files in given directory (and optionally its subdirectories).

Working example:

http://www.programcreek.com/java-api-examples/index.php?class=org.apache.commons.io.FileUtils&method=listFilesAndDirs

Ankur Gupta
  • 301
  • 4
  • 12