This tutorial will shows how to get the file system icon for a specific file.
Getting the Small IconFor all platforms, the FileSystemView class provides a method for getting the small icon (16x16). To get the icon, you must specify a file on the file system for which you want to get an icon. The following code example shows how to use this class to get the icon:
File file = null;
String extension = "doc";
try
{
//Create a temporary file with the specified extension
file = File.createTempFile("icon", "." + extension);
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);
//Delete the temporary file
file.delete();
}
catch (IOException ioe)
{
}
In the example above, a temporary file is created with the extension for the specified document type. This file is passed to the instance of the FileSystemView class to get the icon. This will return the default icon size which is 16x16 pixels on most platforms. Once you have the Icon, you can display it in most Swing controls such as a JLabel, JButton, JMenuItem, etc.
Getting the Large IconUnfortunately, there isn't a standard API call to get the larger icons (32x32) for document types. For platforms using the Sun JVM, there is an undocumented class called sun.awt.shell.ShellFolder that does provide this functionality. The following code is a modified version of our previous example with changes to show how to use the sun.awt.shell.ShellFolder class.
File file = null;
String extension = "doc";
try
{
//Create a temporary file with the specified extension
file = File.createTempFile("icon", "." + extension);
sun.awt.shell.ShellFolder shellFolder = sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(shellFolder.getIcon(true));
//Delete the temporary file
file.delete();
}
catch (Exception e)
{
}
Getting the Small IconFor all platforms, the FileSystemView class provides a method for getting the small icon (16x16). To get the icon, you must specify a file on the file system for which you want to get an icon. The following code example shows how to use this class to get the icon:
File file = null;
String extension = "doc";
try
{
//Create a temporary file with the specified extension
file = File.createTempFile("icon", "." + extension);
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);
//Delete the temporary file
file.delete();
}
catch (IOException ioe)
{
}
In the example above, a temporary file is created with the extension for the specified document type. This file is passed to the instance of the FileSystemView class to get the icon. This will return the default icon size which is 16x16 pixels on most platforms. Once you have the Icon, you can display it in most Swing controls such as a JLabel, JButton, JMenuItem, etc.
Getting the Large IconUnfortunately, there isn't a standard API call to get the larger icons (32x32) for document types. For platforms using the Sun JVM, there is an undocumented class called sun.awt.shell.ShellFolder that does provide this functionality. The following code is a modified version of our previous example with changes to show how to use the sun.awt.shell.ShellFolder class.
File file = null;
String extension = "doc";
try
{
//Create a temporary file with the specified extension
file = File.createTempFile("icon", "." + extension);
sun.awt.shell.ShellFolder shellFolder = sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(shellFolder.getIcon(true));
//Delete the temporary file
file.delete();
}
catch (Exception e)
{
}