如何在JFileChooser中显示文件的默认系统图标? [英] How to display default system icon for files in JFileChooser?

查看:459
本文介绍了如何在JFileChooser中显示文件的默认系统图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JFileChooser 中显示文件的默认系统图标?即 JFileChooser 中的文件图标应该与桌面和资源管理器上显示的图标相同?

How to display default system icon for files in JFileChooser? i.e. the icons of the files in JFileChooser should be the same as the icons that appear on desktop and explorer?

例如,NetBeans图标在桌面上显示的 JFileChooser 中不会显示相同的内容!

For example, NetBeans icon will not appear the same in JFileChooser as it appears on the desktop!

如何操作这个?

推荐答案

我们可以使用 FileSystemView 类来获取它的对象通过调用 getFileSystemView()中的静态方法,然后使用 getSystemIcon()方法,该方法需要文件对象并返回它的图标。

We can use the FileSystemView class and get it's object by calling getFileSystemView() static method in it and then use the getSystemIcon() method which takes a File object and returns it's icon.

FileSystemView FileView 类存在于 javax.swing.filechooser 包中。
文件类位于 java.io 包中。

FileSystemView and FileView classes are present in javax.swing.filechooser package. File class is in the java.io package.

注意: FileSystemView 不会扩展 FileView 。因此,您不能在中使用 FileSystemView 对象jf.setFileView()

Note: FileSystemView does not extend FileView. Hence you cannot use FileSystemView object in jf.setFileView()

JFileChooser jf=new JFileChooser();
jf.setFileView(new MyFileView());
jf.showOpenDialog(this);

class MyFileView extends FileView
{
      public Icon getIcon(File f)
      {
      FileSystemView view=FileSystemView.getFileSystemView();
            return view.getSystemIcon(f);
      }
}

表示当前帧。假设编写此代码的类是 JFrame的子类

this represents the current frame. Assume that the class in which this code is written is sub class of JFrame

或者以简单的方式,

jf.setFileView(new FileView(){
            public Icon getIcon(File f)
            {
                return FileSystemView.getFileSystemView().getSystemIcon(f);
            }
        });

这篇关于如何在JFileChooser中显示文件的默认系统图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆