How to Get selected folder name in java using JFileChooser?

只愿长相守 提交于 2019-12-25 10:16:15

问题


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:


回答1:


JFileChooser#getSelectedFile will return the selected file/directory

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



来源:https://stackoverflow.com/questions/29422453/how-to-get-selected-folder-name-in-java-using-jfilechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!