문제

I'm using JFileChooser to open up a dialog box for a user to open a file. I've already set the current directory if the user actually selects a file:

int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    File newfile = fc.getSelectedFile();

    //set the default directory to this file's directory
    fc.setCurrentDirectory(newfile.getParentFile());
}
else {
    //User cancels file chooser. How to still set the current directory
    //to the one they were last in?
}

However, even if the user cancels the dialog box (e.g. they decide they want to do something else in the program before choosing a file), I want to save the last directory they were in so they avoid the hassle of finding that directory again. Is this possible at all?

도움이 되었습니까?

해결책

This is because an instance of JFileChooser "remembers" it's last location. You could create a new instance each time you want to show the dialog, but that's inefficient and can be time consuming

Instead, save the last "good" location in some kind of instance variable. Before you show the save dialog, set its current directory, passing the last known "good" location

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top