Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFileChooser change default directory in Windows

Tags:

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?

like image 452
dumazy Avatar asked Nov 22 '12 16:11

dumazy


People also ask

How do I find my JFileChooser file name?

JFileChooser fc = new JFileChooser(); int returnVal = fc. showSaveDialog(frame); if (returnVal == JFileChooser. APPROVE_OPTION){ File file = fc. getSelectedFile(); if (file.

What is JFileChooser in Java?

JFileChooser is a part of java Swing package. The java Swing package is part of JavaTM Foundation Classes(JFC) . JFC contains many features that help in building graphical user interface in java . Java Swing provides components such as buttons, panels, dialogs, etc .

What are the commonly used constructors of JFileChooser?

Commonly used Constructors: Constructs a JFileChooser pointing to the user's default directory. Constructs a JFileChooser using the given File as the path. Constructs a JFileChooser using the given path.


2 Answers

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"));
like image 109
Juvanis Avatar answered Oct 21 '22 17:10

Juvanis


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

JFileChooser chooser = new JFileChooser("C:\\Users\\Fre\\Music\\");
like image 30
chou97 Avatar answered Oct 21 '22 16:10

chou97