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?
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"));
why don't you just give the FileChooser the path when you create it, like:
JFileChooser chooser = new JFileChooser("C:\\Users\\Fre\\Music\\");
Sorry for taking your time,
Just found the answer myself:
String userhome = System.getProperty("user.home");
JFileChooser fc = new JFileChooser(userhome +"\\Music");
JFileChooser openFile = new JFileChooser("C:\\Users\\Fre\\Music");
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.
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());
}
Pretty Simple:
JFileChooser browseImageFile = new JFileChooser("User Defined Directory");
Related
I want to use the JFileChooser in Java to get the user to select where they would like to save a file. I have used this code:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
This works and does get the path however the dialog box requires the user to select a file in order to get the file path. As i want it to save a new file i need it to get the path without having to select a file but from the folder the user has selected instead.
Im new to this and not sure of any other way of doing this, please could you advise me on a way around this.
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
I want to change JFileChooser start directory to desktop. So, in my computer I wrote:
JFileChooser fc = new JFileChooser("C:\\Users\\LNK\\Desktop");
The problem is, when I compile my code and run program in another computer it doesn't work because there are no C:\\Users\\LNK\\Desktop path. So, is there some kind of "apsolute" path of desktop?
You can use a user.home system property to get user directory.
So your code would look like
String userDir = System.getProperty("user.home");
JFileChooser fc = new JFileChooser(userDir +"/Desktop");
Here is another option for getting to the Windows desktop:
fileChooser.setCurrentDirectory(new File("C:\\"));
Action details = fileChooser.getActionMap().get("Go Up");
details.actionPerformed(null);
details.actionPerformed(null);
If you leave off the last line, you will get to "Computer"
use this ,
String desktopPath = WindowsUtils.getCurrentUserDesktopPath();
Hi I am trying to make one pane that shows something like windows explorer in my computer.
when user complete it's operations, and after that when he want to save edited image at specific place on disk then he can easily select directory from that pane.
i want to design something like this :
is it possible to do something like that ?
my picture editor looks like :
at right side of editor i want to put something like output directory selection pane.
is anyone know how to do that ?
A complete example using JTree is examined in FileBrowser.
An alternative using Outline is shown here.
Yes its possible. It's basically just JTree.
You will probably want to take a look at File#listRoots, File#isDirectory and File#listFiles.
You'll also want to take a look at How to use trees.
You'll probably also want to take a look at FileSystemView#getSystemIcon which will allow you to look an appropriate icon for the given File
However, it might be simpler to just use a JFileChooser ;)
You could have a look at JFileChooser.
You can use this object to open a SaveDialog and get a save path on the local harddisk.
Then eventually use an ObjectOutputStream to write a file.
Sample code:
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(fb);
if (rVal == JFileChooser.APPROVE_OPTION) {
System.out.println(c.getSelectedFile().toString());
}
This can be handled with a JFileChooser, sorry if it's not the solution you're looking for
Note: you say choose a directory but I assume you mean that they can name their file
private File selectSaveFile() {
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("File Type", "txt"));
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int returnVal = fc.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
}
//the user didn't click save if we are here
return null;
}
I am using Netbeans for java application. during my application at one point i want particular folder URL to store files. how can i achieve this. please can anyone help me..
Thanks in advance :)
Use a JFileChooser, with JFileChooser.DIRECTORIES_ONLY Take a look at this tutorial: How to Use File Choosers
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
You want to select a folder in a swing application, right? you can use JFileChooser http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
to select only a folder, look at this example
http://www.rgagnon.com/javadetails/java-0370.html
for the saving, check
http://download.oracle.com/javase/tutorial/essential/io/file.html
if you need something clarified, just ask.
I guess you want a Open File Dialog box.
In Swing it is called JFileChooser.
Usage example:
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(yourJFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
// Do stuff with file
} else {
// User clicked cancel
}
yourJFrame should be the JFrame you use for your main window. If you don't have one put null.
I am trying to set the directory path in JFilechooser through something like this(using commons-io ) :
String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File theDirectory = new File(fileContents);
filechooser = new JFileChooser();
fileChooser.setCurrentDirectory(theDirectory);
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
I'm using getCanonicalPath() to get the path and write in the file path.txt
path = file.getCanonicalPath();
I don't intend to put all my code here,but I'm sure that the program writes and reads the path in path.txt.
I don't get any error,but everytime I run the program it always open JFilechooser in my documents folder.What i am doing wrong?
Try to pass the current directory directly in the constructor:
filechooser = new JFileChooser(theDirectory);
If you consult the API, using the default constructor (i.e. new JFileChooser()):
Constructs a JFileChooser pointing to
the user's default directory. This
default depends on the operating
system. It is typically the "My
Documents" folder on Windows, and the
user's home directory on Unix.
This would seem to account for always opening to My Documents, but this isn't your problem. In fact, your problem lies with setting the current directory (i.e. setCurrentDirectory(theDirectory)):
Sets the current directory. Passing in
null sets the file chooser to point to
the user's default directory. This
default depends on the operating
system. It is typically the "My
Documents" folder on Windows, and the
user's home directory on Unix. If the
file passed in as currentDirectory is
not a directory, the parent of the
file will be used as the
currentDirectory. If the parent is not
traversable, then it will walk up the
parent tree until it finds a
traversable directory, or hits the
root of the file system.
That being said, I'd pay attention to the highlighted text since it appears that you're setting a file as the current directory and not a directory.
In your main class declare
public static String dirpath=".";
private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser(dirpath);
dirpath =jfc.getSelectedFile().getAbsolutePath().toString();
}
For select the last directory that you open :
chooser.setCurrentDirectory(lastDirectory);
int r = chooser.showOpenDialog(new JPanel());
if (r == JFileChooser.APPROVE_OPTION) {
fileName = chooser.getSelectedFile().getPath();
lastDirectory = chooser.getSelectedFile();
}
JFileChooser Chooser = new JFileChooser("F:");
if you want to change the directory theb use System.getProperty method
String s=System.getProperty("user.dir"); // changes directory from documents to the user current Directory;
JFileChooser jfc=new JFileChooser(s);