JFileChooser does not return all the way

using the following method when you click the Path button:

public static void pathButtonAction() {
    JFileChooser chooser = new JFileChooser();
    if (pathToInbound == null) { //private static File pathToInbound;
    chooser.setCurrentDirectory(new java.io.File("."));
    } else {chooser.setCurrentDirectory(pathToInbound);
            }

    chooser.setDialogTitle("Choose folder with messages to send");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        pathToInbound = chooser.getCurrentDirectory(); 
        addLogText(chooser.getCurrentDirectory().getAbsolutePath());
    }


}

But here I select the folder c: \ windows \ temp Here addLogText (chooser.getCurrentDirectory (). GetAbsolutePath ()) I can only log in to c: \ windows. Why was the temporary folder ignored / truncated?

+3
source share
1 answer

You should call chooser.getSelectedFile()instead chooser.getCurrentDirectory(), this returns the current directory in which the user navigated to filechooser. In your case it is C:\Windows.

+6
source

Source: https://habr.com/ru/post/1779781/


All Articles