1

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^^.

2
  • You can save the directory in another file. Save that file in the user's directory. Use System.getProperty("user.home") to select the user's home directory. Commented Apr 19, 2017 at 19:39
  • I thought so, but the program cant remember the path of that file either, or am I wrong? //EDIT: Nevermind, I'll try doing that. Thank you. :) Commented Apr 19, 2017 at 19:41

1 Answer 1

1

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());

        // ...
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.