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();
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 have been trying for 4 hours now to get this thing to run and I managed to do it without understanding why :/
I have created a very simple java program with a GUI containing 2 textboxes where people can type the path to an exe-file.
When a button is clicked it reads the text in this box and runs the corresponding software.
This seems to work when people type ""C:\Program Files (x86)\thatsoftware\" in the directory box and "C:\Program Files (x86)\thatsoftware\run this.exe -arg" in the file to run box:
Runtime.getRuntime().exec(txtFile.getText().toString(), null, new File(txtPath.getText().toString()));
However, when I set only 1 directory box and append (hardcoded) the file and argument to it, it will not work:
String fileToRun=txtPath.getText().toString()+"run this.exe -arg";
Runtime.getRuntime().exec(fileToRun, null, new File(txtPath.getText().toString()));
I have tried passing the file as an array as well:
String fileToRun[]={txtPath.getText().toString(),"run this.exe"," -arg"};
Runtime.getRuntime().exec(fileToRun, null, new File(txtPath.getText().toString()));
to no avail.
The same kind of problems pop up when I try to run it as a processbuilder.
I will get an error message like "file C:\Program Files (x86)\thatsoftware\ -arg" does not exist." Very weird, since the argument is passed, but not the file name apparently.
I can manage to run it when the whole string is typed in the text box by the user, but not if I add the argument and or filename to it in the code.
Could anyone be so kind to explain this to me and tell how it can be done with only one text box?
As I know you must put each element as separate field:
String fileToRun[]={
txtPath.getText().toString(),
"run",
"this.exe",
" -arg"
};
I don't know if you use Swing or not but Swing has javax.swing.JFileChooser.
//config fileChooser
JFileChooser fc = new JFileChooser(lastOpenDir);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setDialogTitle("Load Beacon List");
fc.removeChoosableFileFilter(fc.getFileFilter()); //remove the default file filter
FileFilter filter = new FileNameExtensionFilter("EXE file", "exe");
fc.addChoosableFileFilter(filter); //add XML file filter
//show dialog
int returnVal = fc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File selectedDir = fc.getSelectedFile();
...
You need to wrap the executable in escaped quotes \" like this:
Runtime runtime = Runtime.getRuntime();
Process ps = runtime.exec("\"run this.exe\"");
Or using a path and the argument as you need:
Process ps = runtime.exec("\"C:\\Program Files (x86)\\Thatsoftware\\my exe.bat\" -arg");
boy do i feel like an idiot...
The java errors threw me off, but the problem was a missing slash before "run this.exe".
so many shames...
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");
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);