Here is how to set default system icon for files and directories in
You can do the same by setting the system look and feel, but the entire UI (including the components) changes. Here is how to set look and feel. Now, you don't need the
If you like this post, share it. You might also like my posts on getting focus for JTextField in NimbusLookAndFeel, deleting a file via JFileChooser and using FileFilter in JFileChooser
JFileChooser
using the simple FileView
. It's nothing more than single line code!. I previously, posted this on Stackoverflow, and now I am happy to share it here. Here are there are two things to talk about. One is giving the default system icon for files using the WindowsLookAndFeel
or the WindowsClassicLookAndFeel
. One more thing, which I will stress out here is setting system icons in a different look and feel. import javax.swing.*;
import java.awt.*;
import javax.swing.filechooser.*;
import java.io.*;
class DefaultSystemIconForJFileChooser extends JFrame
{
JFileChooser jf;
public DefaultSystemIconForJFileChooser()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("Default System icon for JFileChooser");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create a JFileCHooser
jf=new JFileChooser();
// Use the method setFileView. This
// method takes a FileView object. As FileView
// is abstract, anonymous inner class is written
// Override the getIcon() method and return the
// corresponding icon for the given file
jf.setFileView(new FileView(){
public Icon getIcon(File f){
// Get a FileSystemView object using getFileSystemView()
// method (static) of FileSystemView class. This contains
// getSystemIcon() method which takes a file and gives the
// corresponding system icon for it.
return FileSystemView.getFileSystemView().getSystemIcon(f);
}
});
jf.showOpenDialog(this);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new DefaultSystemIconForJFileChooser();
}
});
}
}
You can do the same by setting the system look and feel, but the entire UI (including the components) changes. Here is how to set look and feel. Now, you don't need the
setFileView()
method. try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception e){}
If you like this post, share it. You might also like my posts on getting focus for JTextField in NimbusLookAndFeel, deleting a file via JFileChooser and using FileFilter in JFileChooser