Robin
Robin

Reputation: 2616

How can I get the icon to display for a file from a Java app?

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.

Upvotes: 3

Views: 2538

Answers (2)

Ralph M. Rickenbach
Ralph M. Rickenbach

Reputation: 13163

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.

Upvotes: 9

Jacob Nelson
Jacob Nelson

Reputation: 3006

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" );

Upvotes: 2

Related Questions