views:

124

answers:

3

Is it possible from a Java desktop app to find the icon to display for a given file, perhaps based on it's mime type? Specifically I want to ask the host OS for the icon to display so it can match what the user would expect to see.

A: 

As far as I know, desktop integration in Java 6 goes as far as providing ways to launch files using their associated applications, but I don't know if there is a way of getting the associated icon. You should look at the source code for the Desktop class.

Be aware that if a method exists, it will be platform-dependant.

Altherac
+5  A: 

There is some example here. The relevant code would be:

FileSystemView view = FileSystemView.getFileSystemView();    
Icon icon = view.getSystemIcon(file);

Edit (included comment) The official help page is here.

Ralph Rickenbach
Would be worth linking to here: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/filechooser/FileSystemView.html#getSystemIcon(java.io.File)
Andy Balaam
+1  A: 

AS far as I know the only thing you can do is using the JTree's DefaultTreeCellRenderer. You can read more in the Java API.

I'll give you a few examples here. I haven't used it in a while so you will need to dig a little deeper to get what you want.

UIDefaults defaults = UIManager.getDefaults( );

Icon computerIcon = defaults.getIcon( "FileView.computerIcon" );
Icon floppyIcon   = defaults.getIcon( "FileView.floppyDriveIcon" );
Icon diskIcon     = defaults.getIcon( "FileView.hardDriveIcon" );
Icon fileIcon     = defaults.getIcon( "FileView.fileIcon" );
Icon folderIcon   = defaults.getIcon( "FileView.directoryIcon" );
Jacob Nelson