簡體   English   中英

JFileChooser顯示錯誤的當前目錄

[英]JFileChooser showing wrong current directory

我有以下代碼。 問題是,當我在FileChooser中選擇某個目錄(例如C:\\Windows\\System32 )時,它會打印: C:\\Windows (總是比實際位置低一級)。

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Wybierz folder z logami");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(true);

if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
  path = chooser.getCurrentDirectory().toString();
  jLabel1.setText(path);

getCurrentDirectory()返回當前顯示的目錄的路徑。 如果您在當前目錄中選擇一個子目錄,則將其視為當前選擇的文件

因此,即使您知道它實際上是目錄,也請使用getSelectedFile()

暫無
暫無

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

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