Java JFileChooser>&引用;查阅;奇怪的名字

Java JFileChooser>&引用;查阅;奇怪的名字,java,jfilechooser,Java,Jfilechooser,我有一个在Win7上使用JFileChooser的java应用程序。奇怪的是,有时(经常)但不总是-驱动器名称在“查找范围:”组合框中看起来很奇怪: 有人知道是什么原因导致了这种情况,以及如何使它始终显示专有名称吗?这些名称来自系统驱动器,如我的计算机、网络邻居等 我处理这些文件的方式是: JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileView(new FileView() { @Override

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


有人知道是什么原因导致了这种情况,以及如何使它始终显示专有名称吗?

这些名称来自系统驱动器,如我的计算机、网络邻居等

我处理这些文件的方式是:

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;
   }
});

通过这种方式,它总是提取文件系统显示的名称。

我想分享一段代码,解释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的解决方案。

问题在于,有时(不更改代码)驱动器名称显示正确,有时没有特殊原因,我会看到类似这样的废话:{20D……@Artur是,因此您可以查看
name
是否为空字符串,以及它是否为use
string name=FileSystemView.getSystemTypeDescription(您的文件);
instead@Artur{ 20D垃圾是从不使用FielsSeCyVIEW获取正确的系统名称,空白名称发生是因为<代码>文件.GETNAME()不知道如何翻译系统级驱动器的名称。为了让未来的读者更清楚地了解您的答案,请添加相关信息。为了使用您的方法,您必须为JFileChooser设置新的FileView,然后主要重写其getName方法。顺便说一句,FileSystemView是一个抽象类,因此要使其工作,您必须:比如“String name=FileSystemView.getFileSystemView().getSystemDisplayName(yourFile);”,然后我会接受你的答案,这样你就可以得到你应得的更多荣誉。