Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...