JFileChooser> “看看”奇怪的名字

时间:2015-02-19 13:40:07

标签: java jfilechooser

我有一个在Win7上使用JFileChooser的java应用程序。奇怪的是,有时候(经常)但并不总是 - 驱动器名称看起来很奇怪。查看:'组合框:

enter image description here

有没有人知道是什么原因以及如何让它始终显示正确的名称?

2 个答案:

答案 0 :(得分:5)

那些来自系统驱动器,如我的电脑,网络邻居等

我绕过它展示这样的文件的方式是:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileView(new FileView() {

   @Override
   public String getName(File f) {
      String name = FileSystemView.getFileSystemView().getSystemDisplayName(f);

      // If names is empty use the description
      if(name.equals("")) {
         name = FileSystemView.getFileSystemView().getSystemTypeDescription(f);
      }

      return name;
   }
});

这样它总是拉动文件系统显示的名称。

答案 1 :(得分:2)

我想分享一小段代码,解释JFileChooser不那么明显的行为。

在Windows上,如果您在CMD会话或Windows文件浏览器中导航文件系统,则会有所不同。

例如,您导航到驱动器c:\的根目录。

<强> CMD

rem this will still stay in C:\ as there is no parent directory
cd ..

Windows文件浏览器

- the parent directory of 'C:\' is 'Computer'
- but 'Computer' is not a real directory and is accessed by an CLSID (Class Identifier
  for COM class objects), the incomprehensible '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}'

使此行为更清晰的代码。

import java.io.File;
import javax.swing.filechooser.FileSystemView;

public class JFileChooserParentDirectory {

    public static void main(String[] args) {
        // the root directory of drive C:
        File file = new File("C:/");

        // get a view of the file system
        FileSystemView fileSystemView = FileSystemView.getFileSystemView();

        // get the parent directory of our file
        File parentDir = fileSystemView.getParentDirectory(file);

        // get the Windows display name of this parent directory
        String displayName = fileSystemView.getSystemDisplayName(parentDir);

        // get the Windows type description of this parent directory
        String typeDescription = fileSystemView.getSystemTypeDescription(parentDir);

        // print out all the different values
        String printFormat = "%-50s: %s%n";
        System.out.printf(printFormat, "file.getAbsolutePath()", file.getAbsolutePath());
        System.out.printf(printFormat, "parentDir.getName()", parentDir.getName());
        // this absolute path is related to the directory from which you started the code
        System.out.printf(printFormat, "parentDir.getAbsolutePath()", parentDir.getAbsolutePath());
        System.out.printf(printFormat, "fileSystemView.getSystemDisplayName(parentDir)", displayName);
        System.out.printf(printFormat, "fileSystemView.getSystemTypeDescription(parentDir)", typeDescription);
    }
}

打印出来。

file.getAbsolutePath()                            : C:\
parentDir.getName()                               : ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
parentDir.getAbsolutePath()                       : D:\development\stackoverflow\playground\::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
fileSystemView.getSystemDisplayName(parentDir)    : Computer
fileSystemView.getSystemTypeDescription(parentDir): System Folder

要解决JFileChooser中的问题,请从@inquisitor获取解决方案。