How to display default system icon for files in JFileChooser?
Asked Answered
P

2

6

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?

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

How to do this?

Pleat answered 13/7, 2013 at 12:11 Comment(0)
P
7

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 and FileView classes are present in javax.swing.filechooser package. File class is in the java.io package.

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

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

Or in a simple way,

jf.setFileView(new FileView(){
            public Icon getIcon(File f)
            {
                return FileSystemView.getFileSystemView().getSystemIcon(f);
            }
        });
Pleat answered 13/7, 2013 at 12:11 Comment(0)
M
6

The way shown by @JavaTechnical is one way. Here is another (easier) way. Set the GUI (or at least the file chooser) to the native PLAF. E.G.

File chooser with native 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 https://mcmap.net/q/120823/-how-to-best-position-swing-guis 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);
    }
}

Of course, if you are feeling brave, you might create a custom file chooser starting with something like the File Browser GUI.

Manufacture answered 13/7, 2013 at 12:30 Comment(5)
Hmm. That is good! What if i want to change the look and feel, say I love NimbusLookAndFeel! ;)Pleat
@Pleat "I love NimbusLookAndFeel" You love bugs? Nimbus is one of the most bug ridden PLAFs ever developed. ..And that is saying something!Manufacture
+1 OK. By the way how do you feel about the way it looks and the way it can be customized? :)Pleat
Umm.. I have no strong opinions or feelings on that. I've never tried customizing a JFileChooser (beyond the obvious 'PLAF').Manufacture
I warmly remember that program and its development! 1+Hoopes

© 2022 - 2024 — McMap. All rights reserved.