O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Get Large File Icons #99
Chapter 12, Miscellany
|
499
HACK
H A C K
#99
Get Large File Icons
Hack #99
Using an undocumented Windows-only class, you can retrieve large, full-
color file icons from the operating system.
The
FileSystemView
provides access only to file icons of a default size, which
usually means 16 × 16 pixels. If you look at your desktop, however, you may
see icons that are much bigger and with more detail and color. There is no
standard way to get the larger icons, but on Windows you can use an
undocumented (and unsupported) class to get access to them. Sun’s JRE for
Windows includes a hidden class called
sun.awt.shell.ShellFolder that will
let you retrieve larger (32 × 32) desktop file icons.
This class is only available in Sun’s JRE for Windows, so it
won’t work with other vendors or on other platforms.
The class in Example 12-22 will take a filename and show its large icon in a
window.
Example 12-22. Grabbing a large icon
public class LargeIconTest {
public static void main(String[] args) throws Exception {
// Create a File instance of an existing file
File file = new File(args[0]);
// Get metadata and create an icon
sun.awt.shell.ShellFolder sf =
sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));
System.out.println("type = " + sf.getFolderType( ));
// show the icon
JLabel label = new JLabel(icon);
JFrame frame = new JFrame( );
frame.getContentPane( ).add(label);
frame.pack( );
frame.show( );
}
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required