简体   繁体   中英

How to Get selected folder name in java using JFileChooser?

I want to select the folder that is selected.

 JFileChooser targetDir = new JFileChooser();
            targetDir.setDialogTitle("Choose Target Directory.");
            targetDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if(targetDir.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)   
            {
                System.out.println(targetDir.getCurrentDirectory());
                main_mw = new MainWindow("XYZ Copier");
            main_mw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                        } else {
                System.exit(0);
            }
        } else {
        }

It gives the output "/home/rahul/Downloads/mc" but I need "/home/rahul/Downloads/mc/lib". It gives same result if i go inside lib.

Screenshots:

JFileChooser#getSelectedFile will return the selected file/directory

getCurrentDirctory returns the directory which is currently been shown in the chooser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM