Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the icon for a file extension or filetype without creating a temp file?

I list some filenames with their icons (like the ones in the Windows Explorer) in a JTable. I know the two ways to get the icon if I have a File object from the local file system:

javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( file ) 

for a 16x16 icon or for a bigger one:

sun.awt.shell.ShellFolder.getShellFolder( file ).getIcon( true ) ) 

Since my files are stored in a database, I don't have the File object. My workaround is to create a temp file with the specific filename extension, use one of the two methods above and cache the icon to display it in a CellRenderer.

I searched for a solution without temporary files and found two I don't like either:

  • org.eclipse.swt.program.Program.findProgram(String extension).getImageData(), but I don't want to use SWT
  • org.jdesktop.jdic.icons.IconService from the Incubator of the JDIC project. The last changes on the IconService are 6 years ago, on JDIC 2 years ago and I can't find a downloadable jar.

Is there another solution?

like image 423
bobndrew Avatar asked Dec 15 '11 14:12

bobndrew


People also ask

How do I assign an icon to a file type?

Right click extension whose icon you want to change and then select “Edit Selected File Type.” In the “Edit File Type” window, click the “…” button to the right of the Default Icon text field. The “Change Icon” window shows some basic icons, but click the “Browse” button to find your own icon files.

What file types can be used as icons?

The ICO file format is an image file format for computer icons in Microsoft Windows.

What file extension does an icon have?

ICON files are typically saved as small, square bitmap images, ranging from 16 x 16 to 256 x 256 pixels in size. Most Windows Icon files use the . ICO extension.


1 Answers

Looks like you already discovered the way to do it, unless you want to dive into native libraries etc.

FileSystemView uses Win32ShellFolder internally so they are basically the same.

I also dug up the Source for org.eclipse.swt.program.Program and with it org.eclipse.swt.internal.win32.OS. The OS class then uses a native call for the Icon. At this point unless you really really cannot create a Temp File i would not go down that path.

For JDIC i only found http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm with a little bit of digging(may not be related but does icony things :D). Also calls native.

like image 52
Stefan Avatar answered Sep 19 '22 12:09

Stefan