问题
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 SWTorg.jdesktop.jdic.icons.IconService
from the Incubator of the JDIC project. The last changes on theIconService
are 6 years ago, on JDIC 2 years ago and I can't find a downloadable jar.
Is there another solution?
回答1:
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.
回答2:
Do you really need the temporary file to use the first option? A File does not have to denote a file that actually exists...
来源:https://stackoverflow.com/questions/8521294/how-to-get-the-icon-for-a-file-extension-or-filetype-without-creating-a-temp-fil