簡體   English   中英

如何使用JFileChooser在Java中獲取選定的文件夾名稱?

[英]How to Get selected folder name in java using JFileChooser?

我要選擇所選的文件夾。

 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 {
        }

它給出的輸出是“ / home / rahul / Downloads / mc”,但我需要“ / home / rahul / Downloads / mc / lib”。 如果我進入lib,它會給出相同的結果。

截圖:

JFileChooser#getSelectedFile將返回選定的文件/目錄

getCurrentDirctory返回當前顯示在選擇器中的目錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM