でファイルのデフォルトのシステム アイコンを表示するJFileChooser方法 つまり、ファイルのアイコンは、JFileChooserデスクトップやエクスプローラーに表示されるアイコンと同じにする必要がありますか?
たとえば、NetBeans アイコンはJFileChooser、デスクトップに表示されるものと同じには表示されません!
これを行う方法?
でファイルのデフォルトのシステム アイコンを表示するJFileChooser方法 つまり、ファイルのアイコンは、JFileChooserデスクトップやエクスプローラーに表示されるアイコンと同じにする必要がありますか?
たとえば、NetBeans アイコンはJFileChooser、デスクトップに表示されるものと同じには表示されません!
これを行う方法?
クラスを使用して、静的メソッドをFileSystemView呼び出してそのオブジェクトを取得し、オブジェクトを取得してそのアイコンを返すメソッドを使用できます。getFileSystemView()getSystemIcon()File
FileSystemViewFileViewクラスはjavax.swing.filechooserパッケージ
に含まれています。Fileクラスは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);
}
}
this現在のフレームを表します。このコードが書かれているクラスが のサブクラスであるとします。JFrame
または、簡単な方法で、
jf.setFileView(new FileView(){
public Icon getIcon(File f)
{
return FileSystemView.getFileSystemView().getSystemIcon(f);
}
});
@JavaTechnical が示す方法は 1 つの方法です。これが別の(より簡単な)方法です。GUI (または少なくともファイル チューザー) をネイティブ 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);
}
}
もちろん、勇気があれば、 File Browser GUIのようなカスタム ファイル チューザーを作成することもできます。
