Java selects file location

I am not sure if this is possible, since I can’t find anything about it after several Google searches.

What I would like to do is open the event in the file dialog and allow the user to select the folder, and then store the full directory in the folder in a row. Therefore, if the user selected a folder in C: \ Windows \ Example, the directory will be stored in String fileDir = C: \ Windows \ Example;

It makes sense? Hope I'm struggling to find the answer. I do apperciate help, thanks in advance for watching and thanks if you help me :)
+3
source share
2 answers

In a swing you will need JFileChooser .

public String promptForFolder( Component parent )
{
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );

    if( fc.showOpenDialog( parent ) == JFileChooser.APPROVE_OPTION )
    {
        return fc.getSelectedFile().getAbsolutePath();
    }

    return null;
}

. , . , DirectoryChooser. , ; .

+8

FileChooser.

, , . , API JFileChooser , .

+3

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


All Articles