4

I'm not sure if this is even possible as I can't find anything about it after quite a few Google searches.

What I would like to do is on event open up a file dialog box and allow the user to select a folder and then store that folders full directory in a string. So if a user selected a folder in C:\Windows\Example the directory would be stored in String fileDir = C:\Windows\Example;

Does this make sense? I hope so as I'm struggeling to find the answer. I do apperciate the help, thanks in advance for looking and more thanks if you help me :)

1
  • Are you using Swing? Can you show some example code where you'd like the interaction to happen? It is definitely possible to browse the local file system using a Java desktop app.
    – Matt Ball
    Feb 1, 2011 at 3:14

2 Answers 2

8

In swing you'll want a JFileChooser.

public String promptForFolder( Component parent )
{
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );

    if( fc.showOpenDialog( parent ) == JFileChooser.APPROVE_OPTION )
    {
        return fc.getSelectedFile().getAbsolutePath();
    }

    return null;
}

It can be a little awkward selecting folders from a user's perspective. I've watched a lot of folks struggle with it. If you have the time you may want to try my DirectoryChooser. Sorry the code is so crufty; I wrote it awhile back.

6
  • Hey, thanks for this only have only problem when I call... PrefetchAudio pa = new PrefetchAudio(); pa.PromptForFolder(); I get "PromptForFolder(java.awt.Component) in PrefetchAudio cannot be applied to ()" any ideas I'm baffled as you don't need to parse anything to it?
    – James
    Feb 1, 2011 at 4:00
  • @James, null may be passed if you do not have a visible swing component. Feb 1, 2011 at 12:32
  • 3
    @camickr, My answer is not quite a duplicate. It provides a little more help than the other by providing actual source code. This sort of thing was recommended by Jeff in the podcasts. Improve an existing answer or combine multiple answers to make them your own. Feb 1, 2011 at 12:38
  • 2
    And everyone else that comes to this site looking for a quick answer to a pretty specific question must now go read the tutorials? I'm sorry, but I'm not convinced. The MYYN is free to take my example or introduce his/her own. In that case, I would delete mine because... You know what? This is petty. My answer stands. Deal. Feb 1, 2011 at 18:08
  • 1
    @JosephGordon +1. Duplicate Answer with more substance and isn't just one sentence and a quote.. ;) Jun 19, 2013 at 13:38
3

You are looking for a FileChooser.

File choosers provide a GUI for navigating the file system, and then either choosing a file or directory from a list, or entering the name of a file or directory. To display a file chooser, you usually use the JFileChooser API to show a modal dialog containing the file chooser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.