20

I want to change the default directory of my JFileChooser to "My Music" on Windows. This directory is C:\Users\Fre\Music on my account because my username is Fre

The default is set on C:\Users\Fre\Documents (depends on OS i think). How can I change this?

dumazy
  • 13,857
  • 12
  • 66
  • 113
  • Be aware that a user's Music folder is not necessarily at the directory path you mention. It can be changed easily by the user. See http://www.techsupportalert.com/content/how-move-windows-7-personal-folders-my-documents-another-drive.htm – Klitos Kyriacou Jan 05 '16 at 12:25

7 Answers7

39

You can use the API method setCurrentDirectory when initializing your JFileChooser objects:

public void setCurrentDirectory(File dir)

Sample usage might be like:

yourFileChooser.setCurrentDirectory(new File  
(System.getProperty("user.home") + System.getProperty("file.separator")+ "Music"));
brad
  • 930
  • 9
  • 22
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • yes, but to which directory? because now I'm running it on my account but the application will have another username on another account – dumazy Nov 22 '12 at 16:33
  • 2
    I think you actually meant System.getProperty("file.separator"), not "line.separator". Line separator makes the string go on a new line. – wiredmark Jan 05 '15 at 14:43
13

why don't you just give the FileChooser the path when you create it, like:

JFileChooser chooser = new JFileChooser("C:\\Users\\Fre\\Music\\");
Shell
  • 6,818
  • 11
  • 39
  • 70
chou97
  • 131
  • 1
  • 2
4

Sorry for taking your time, Just found the answer myself:

String userhome = System.getProperty("user.home");
JFileChooser fc = new JFileChooser(userhome +"\\Music");
dumazy
  • 13,857
  • 12
  • 66
  • 113
  • 1
    I suggest you to use line.separator as well, what if your user is a linux user? – Juvanis Nov 22 '12 at 16:40
  • Not really sure about that. Perhaps I can first check the OS it's running on, and then decide what directory to use. – dumazy Nov 22 '12 at 16:43
4
JFileChooser openFile = new JFileChooser("C:\\Users\\Fre\\Music");
Gary
  • 13,303
  • 18
  • 49
  • 71
Ambure
  • 51
  • 1
0

Creating all your own code, so as to set a default file directory is unnecessary and lengthy. A much easier and quicker way of doing it is by just right clicking on the File Chooser itself on Design view and right clicking 'customise code'.

Customise Code for File Chooser

This will show you the vital code for that GUI component. From the drop down box next to the top line of code, select 'custom creation'.

This will allow you to customise what fileChooser = is assigned to. Between the curly brackets JFileChooser() you can either hard code in the file directory with speech marks like this.

JFileChooser("C:\Users\user\Documents")

or type in a name that for a variable you created earlier. This variable would hold the file directory. I would recommend the latter option, though either will work fine.

Hope this helps.

p.s. sorry about having to use a link for the photo. I don't have enough privilege yet.

George T 97
  • 41
  • 1
  • 7
0

You can Change the default directory of my JFileChooser to "Directory you want" on Windows

JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("put here your directory"));
int result = fileChooser.showOpenDialog(getParent());
if (result == JFileChooser.APPROVE_OPTION) 
{
    File selectedFile = fileChooser.getSelectedFile();
    jTextField.setText(selectedFile.getAbsolutePath());
}
Swapnil
  • 2,409
  • 4
  • 26
  • 48
0

Pretty Simple:

JFileChooser browseImageFile = new JFileChooser("User Defined Directory");
xiawi
  • 1,772
  • 4
  • 19
  • 21