如何在JFileChooser中显示文件的默认系统图标?

Jav*_*cal 6 java swing jfilechooser

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

例如,NetBeans图标JFileChooser与桌面上显示的图标不同!

这该怎么做?

Jav*_*cal 7

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

FileSystemView包中有FileView类和类javax.swing.filechooser. Fileclass在java.io包中.

注意: FileSystemView不延长FileView.因此你不能使用FileSystemView对象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);
      }
}
Run Code Online (Sandbox Code Playgroud)

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

或者以一种简单的方式,

jf.setFileView(new FileView(){
            public Icon getIcon(File f)
            {
                return FileSystemView.getFileSystemView().getSystemIcon(f);
            }
        });
Run Code Online (Sandbox Code Playgroud)


And*_*son 6

@JavaTechnical显示的方式是一种方式.这是另一种(更简单)的方式.将GUI(或至少文件选择器)设置为本机PLAF.例如

文件选择器与原生PLAF

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FileChooserIcons {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                    e.printStackTrace();
                }
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(20, 30, 20, 30));

                JButton browse = new JButton("Show File Chooser");
                final JFrame f = new JFrame("File Chooser");
                ActionListener showChooser = new ActionListener() {

                    JFileChooser jfc = new JFileChooser();

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jfc.showOpenDialog(f);
                    }
                };
                browse.addActionListener(showChooser);
                gui.add(browse);

                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,如果你感觉很勇敢,你可以创建一个自定义文件选择器,从文件浏览器GUI开始.

  • @JavaTechnical*"我喜欢NimbusLookAndFeel"*你喜欢虫子吗?Nimbus是有史以来开发最多的错误之一...那是在说些什么! (3认同)