Returns a system specific icon of a file. - Java 2D Graphics

Java examples for 2D Graphics:Icon

Description

Returns a system specific icon of a file.

Demo Code

import java.awt.Image;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

public class Main{
    /**//from  w ww.  j  a v  a2s.  c  om
     * Returns a system specific icon of a file.
     *
     * @param  file file
     * @return icon or null on errors
     */
    public static Icon getSystemIcon(File file) {
        if (file == null) {
            throw new NullPointerException("file == null");
        }
        Icon icon = null;
        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
        if (file.exists()) {
            synchronized (fileSystemView) {
                try {
                    icon = fileSystemView.getSystemIcon(file);
                } catch (Throwable t) {
                    Logger.getLogger(IconUtil.class.getName()).log(
                            Level.SEVERE, null, t);
                }
            }
        }
        return icon;
    }
}

Related Tutorials