I have several dialog boxes which provide a File Chooser. For the first, my coding was like this
JFileChooser chooser= new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal= chooser.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
File f= chooser.getSelectedFile();
jTextField1.setText(f.getPath());
chooser.setCurrentDirectory(f);
}
In my case, i would like to set the last path which is selected as the default path in next selection JFileChooser. Is there any solution for me?
Thanks for any response
Depending on your requirements, you can use Preferences to store it away and use it again after the program has been restarted.
Preferences pref = Preferences.userRoot();
// Retrieve the selected path or use
// an empty string if no path has
// previously been selected
String path = pref.get("DEFAULT_PATH", "");
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// Set the path that was saved in preferences
chooser.setCurrentDirectory(new File(path));
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
chooser.setCurrentDirectory(f);
// Save the selected path
pref.put("DEFAULT_PATH", f.getAbsolutePath());
}
You will have to "remember" the last path.
This can easily be done by storing the value in a instance variable...
private File lastPath;
//...
lastPath = f.getParentFile();
And simply resetting it when you need to...
//...
if (lastPath != null) {
chooser.setCurrentDirectory(lastPath);
}
You could also use a single instance of the JFileChooser, so each time you show it, it will be at the last location it was used...
Related
How can I get the absolute path of a directory using JFileChooser, just selecting the directory?
Use:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//or
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
together with:
chooser.getCurrentDirectory()
//or
chooser.getSelectedFile();
then call getAbsoluteFile() on the File object returned.
JFileChooser's getSelectedFile() method, returns a File object.
Use the getAbsolutePath() to get the absolute name to the file.
modified example from the javadoc:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
chooser.getSelectedFile().getAbsolutePath());
}
Try:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = chooser.getSelectedFile();
String fullPath = file.getAbsolutePath();
System.out.println(fullPath);
fullPath gives you the required Absolute path of the Selected directory
So I wanted to save files needed for the program, but the user has to decide where to save files... But because I dont want the user to select the path everytime he starts the program, the program should save the path where to go to find the files, how to?
I made the user select the file with a JFileChooser.
JButton jButton = new JButton();
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
jFileChooser.setDialogTitle("Choose your Path!");
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(jFileChooser.showOpenDialog(jButton) == JFileChooser.APPROVE_OPTION){
}
path = jFileChooser.getSelectedFile().getAbsolutePath();
pathBind = jFileChooser.getSelectedFile().getAbsolutePath() + "/binds.jar";
bindFile = new File(pathBind);
If any further information is needed, I'll give it... Sorry if I forgot something^^.
That kind of information probably belongs in user preferences, which you access using the Preferences class:
private static final String LAST_SAVE_DIR_PREFS_KEY = "last-save-dir";
private static final Preferences PREFERENCES =
Preferences.userNodeForPackage(MyUserInterface.class);
// ...
String saveDir = PREFERENCES.get(LAST_SAVE_DIR_PREFS_KEY,
System.getProperty("user.home"));
jFileChooser.setSelectedFile(new File(saveDir));
jFileChooser.setDialogTitle("Choose your Path!");
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jFileChooser.showOpenDialog(jButton) == JFileChooser.APPROVE_OPTION) {
PREFERENCES.put(LAST_SAVE_DIR_PREFS_KEY,
jFileChooser.getSelectedFile().toString());
// ...
}
In my java application, there is a browse button. When browse button is clicked, popup a file chooser to select a file. When I close the file chooser by clicking cross mark at the top right corner without selecting a file, it gives an Exception saying "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException". How shall I prevent this error?
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
//chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//chooser.setAcceptAllFileFilterUsed(false);
chooser.showOpenDialog(frame);
path=chooser.getSelectedFile().getPath();
If you exit the JFileChooser without selecting a file, chooser.getSelectedFile() will return null.
Therefore, on your line path=chooser.getSelectedFile().getPath(); you are getting a NullPointerException when you try to call getPath() on the null selected file, since you exited.
You will need to do some error handling, such as this:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.showOpenDialog(frame);
File selectedFile = chooser.getSelectedFile();
if (selectedFile == null) {
System.out.println("No file selected!");
path = "";
}
else {
path = selectedFile.getPath();
}
In situations like this, I'd recommend reading through the Javadoc of the method you're retrieving resources from. Quite often under the "returns" section it will state if the returned object can be null, or even if it is guarenteed not to be null.
It helps me a lot when deciding when and when not to add things like null checking.
Probably the best way to go with JFileChooser is using chooser.showOpenDialog(this), which returns a value that indicates what the user clicked.
Instead of
chooser.showOpenDialog(frame);
you can write
int returnVal = chooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
path=chooser.getSelectedFile().getPath();
// whatever other code that only has sense if the user clicked "Ok".
}
And now you understood it, the usual faster way:
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //whatever }
You can take a look at the other few values chooser.showOpenDialog() can return, but this is usually enough.
I have two seperate methods of opening a file.
The first uses a FileChoser with an additional file type filter.
JFileChooser inFileName = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("PCF & TXT Files", "pcf", "txt");
inFileName.setFileFilter(filter);
Component parent = null;
int returnVal = inFileName.showOpenDialog(parent);`
The second uses a JOptionPane but has a loop to ensure the directory chosen exists
String filePath;
File directory;
do{
filePath = JOptionPane.showInputDialog("please enter directory");
directory = new File(filePath);
if (directory.exists()==false){
JOptionPane.showMessageDialog(null,"error with directory");
}
}while(directory.exists()==false);
I'm looking to get the best of both here. To be able to choose a file, using a file filter and also loop that function should that directory not be valid.
I've tried switching around variable names and the various functions in different places but I cant seem to get the loop (".exists" function) to work.
You just need to modify your JFileChooser code to use a loop.
JFileChooser inFileName = new JFileChooser();
File file;
boolean valid = false;
while (!valid) {
int returnVal = inFileName.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = inFileName.getSelectedFile();
valid = file.isDirectory();
else {
valid = returnVal == JFileChooser.CANCEL_OPTION;
}
}
Its worth mentioning that this kind of thing might be better achieved using;
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
I'm using the JFileChooser to allow a user to choose a .txt file that will later be processed by my program, however when the user chooses the file, it is actually opened by my computers default app (in my case TeXworks) as well as used by my program. Any idea how I can stop this?
File fileToOpen = fileChooser.getSelectedFile();
JFileChooser's getSelectedFile() method, returns a File object.
Use the getAbsolutePath() to get the absolute name to the file.
Modified example from the JavaDoc:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
System.out.println("You chose to open this directory: " + chooser.getSelectedFile().getAbsolutePath());
}
So in your case you just need to append .getAbsolutePath() to the end of your statement, like this:
File fileToOpen = fileChooser.getSelectedFile().getAbsolutePath();