でファイルのカスタム アイコンを表示するJFileChooser方法 まあ、ファイルのデフォルトのシステムアイコンも、に付属のデフォルトのアイコンも必要ありませんJFileChooser。自分のアイコンが欲しい。
拡張子でファイルのアイコンを設定したい。どうすればそれができますか?
でファイルのカスタム アイコンを表示するJFileChooser方法 まあ、ファイルのデフォルトのシステムアイコンも、に付属のデフォルトのアイコンも必要ありませんJFileChooser。自分のアイコンが欲しい。
拡張子でファイルのアイコンを設定したい。どうすればそれができますか?
タイプHashtableとして拡張子を含むを利用できます。StringImageIcon
Hashtablejava.utilパッケージに入っています
FileViewjavax.swing.filechooserパッケージに入っています
// Create a hashtable for String,ImageIcon
Hashtable<String,ImageIcon> table=new Hashtable<>();
    // Add extensions and icons
    table.put(".txt",new ImageIcon("txtfile.png"));
    table.put(".doc",new ImageIcon("docfile.png"));
    table.put(".ppt",new ImageIcon("pptfile.png"));
    table.put(".lnk",new ImageIcon("link.png"));
    table.put(".png",new ImageIcon("image.png"));
    table.put(".gif",new ImageIcon("image.png"));
    table.put(".jpeg",new ImageIcon("image.png"));
    table.put(".jpg",new ImageIcon("image.png"));
クラスでMyFileView
class MyFileView extends FileView
{
Hashtable<String,ImageIcon> table;
ImageIcon dirIcon;
    public MyFileView(Hashtable<String,ImageIcon> table,ImageIcon dirIcon)
    {
        this.table=table;
        this.dirIcon=dirIcon;
    }
    public Icon getIcon(File f)
    {
    // Do display custom icons
        // If dir
        if(f.isDirectory()) 
        {
            if(dirIcon!=null) return dirIcon;
        return new ImageIcon("myfoldericon.png");
        }
        // Get the name
        String name=f.getName();
        int idx=name.lastIndexOf(".");
        if(idx>-1)
        {
        String ext=name.substring(idx);
            if(table.containsKey(ext))
        return table.get(ext);
        }
    // For other files
    return new ImageIcon("myownfileicon.png");
    }
}
そして、これを使用するコードは、
MyFileView m=new MyFileView(table,new ImageIcon("diricon.png"));
JFileChooser jf=new JFileChooser();
jf.setFileView(m);
jf.showOpenDialog(this);
拡張機能を使用したくない場合、またはハード ドライブ (私のコンピューター) にカスタム アイコンを設定したい場合は、UI の既定値を使用できます。