Is there any way to make Java file selection dialogs remember the last directory?
Asked Answered
M

5

22

When using Java applications, every time I open a dialog box, the starting directory is always my home directory. Is there any way to make it remember the last used directory?

Alternately, are there improved Java file selection dialogs that allow type-to-search or any of the features that are standard in most other application file selection dialogs?

EDIT: I think the answers posted address the question for writing Java applications, but not for a user. Perhaps it's not possible for the user to change the file browser interface, but I'd like to know that. In case it matters, I have a few specific examples in mind (Amazon AWS uploader) but I have observed that behavior in most Java applications that use a file browser.

Murry answered 26/11, 2011 at 22:17 Comment(0)
L
15

Store the file chooser as a class attribute. When it is re-opened, the following will be preserved.

  1. The directory.
  2. The place in the directory to where the user had scrolled.
  3. The selected file filter.
  4. The size.
  5. The location on screen.
  6. The PLAF.
  7. ..

Is there any way to make it remember the last used directory?

Of course, if you mean persist the state between runs, there are a number of alternative forms of storing the details, and places/ways to store them. See this answer for an example of storing the bounds of a JFrame using a Properties file.


Perhaps it's not possible for the user to change the file browser interface, ..

What 'user'? Do you mean the developer who uses one in an app.?

Maybe what you need is to implement your own file chooser. If that is the case, you might start with the FileBro code.

Lysin answered 26/11, 2011 at 23:56 Comment(3)
+1 so simple. In other words, only create one and just reuse it over and over so it retains its state (selection, location, etc.)Classmate
By 'user', I meant end-user, not developer. I am frequently forced to use java applications written by others that have file browser interfaces I don't like. I suppose if I can get access to the source, I can change the file browser and repackage the code, so to that end your answer is pretty helpful.Murry
Size and position are reset on every showOpenDialog for me (Java 17) when JFileChooser is kept as a field. The location is persisted indeed.Ruddle
M
42

JFileChooser does not remember it. However, Java provides a Preferences API

Preferences prefs = Preferences.userRoot().node(getClass().getName());
JFileChooser chooser = new JFileChooser(prefs.get(LAST_USED_FOLDER,
    new File(".").getAbsolutePath()));
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    // do something
    prefs.put(LAST_USED_FOLDER, chooser.getSelectedFile().getParent());
}
Mamey answered 25/3, 2014 at 15:29 Comment(2)
where is defined LAST_USED_FOLDER?Overview
it's a constant. Just define one.Mamey
L
15

Store the file chooser as a class attribute. When it is re-opened, the following will be preserved.

  1. The directory.
  2. The place in the directory to where the user had scrolled.
  3. The selected file filter.
  4. The size.
  5. The location on screen.
  6. The PLAF.
  7. ..

Is there any way to make it remember the last used directory?

Of course, if you mean persist the state between runs, there are a number of alternative forms of storing the details, and places/ways to store them. See this answer for an example of storing the bounds of a JFrame using a Properties file.


Perhaps it's not possible for the user to change the file browser interface, ..

What 'user'? Do you mean the developer who uses one in an app.?

Maybe what you need is to implement your own file chooser. If that is the case, you might start with the FileBro code.

Lysin answered 26/11, 2011 at 23:56 Comment(3)
+1 so simple. In other words, only create one and just reuse it over and over so it retains its state (selection, location, etc.)Classmate
By 'user', I meant end-user, not developer. I am frequently forced to use java applications written by others that have file browser interfaces I don't like. I suppose if I can get access to the source, I can change the file browser and repackage the code, so to that end your answer is pretty helpful.Murry
Size and position are reset on every showOpenDialog for me (Java 17) when JFileChooser is kept as a field. The location is persisted indeed.Ruddle
A
9

Swing JFileChooser has an method to set the initial directory. setCurrentDirectory(File dir) After the file has been chosen you can get the selected directory by calling getCurrentDirectory and store it in some config file of your app.

Adrea answered 26/11, 2011 at 22:30 Comment(2)
That's a low-level function, right? Is there any way to access and modify those variables as a user?Murry
This is not as good a solution as retaining a reference to the chooser. See my answer.Lysin
P
2

You can cache the last dir location in application and while opening the file chooser specify the default location by setCurrentDirectory

Peake answered 26/11, 2011 at 22:20 Comment(5)
I assume you are talking on Swing.Peake
This is not as good a solution as retaining a reference to the chooser. See my answer.Lysin
@Andrew its not about retaining the instance of filechooser its about just retaining a locationPeake
Think 'outside the box'. Sometimes people don't know what is possible so don't quite know what they are asking for.Lysin
@Andrew You can cache the last dir location in applicatio isn't this clearly telling that ??Peake
A
0

Here i used a simple code with directory dialog to open with last use directory

IDialogSettings dialogSettings = Activator.getDefault().getDialogSettings(); 
    String lastUsedPath = dialogSettings.get(IAntUIConstants.DIALOGSTORE_LASTANTHOME);
    if (lastUsedPath == null) {
        lastUsedPath = "c:\\";
    }
    DirectoryDialog dialog = new DirectoryDialog(Display.getDefault().getActiveShell());
    dialog.setFilterPath(lastUsedPath);
    String location = dialog.open();
    if (location == null) {
        return;
    }
    dialogSettings.put(IAntUIConstants.DIALOGSTORE_LASTANTHOME, location); 
      AntCorePreferences preferences = AntCorePlugin.getPlugin().getPreferences(); 
      String defaultHome = preferences.getAntHome(); 
      if (location.equalsIgnoreCase(defaultHome)) { 
          location = null; 
      } 
Accordingly answered 17/7, 2017 at 4:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.