如何在不创建临时文件的情况下获取文件扩展名或文件类型的图标?

时间:2021-02-02 23:55:28

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:

我在JTable中列出了一些带有图标的文件名(如Windows资源管理器中的那些)。如果我有一个来自本地文件系统的File对象,我知道获取图标的两种方法:

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

for a 16x16 icon or for a bigger one:

对于16x16图标或更大的图标:

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.

由于我的文件存储在数据库中,因此我没有File对象。我的解决方法是创建具有特定文件扩展名的临时文件,使用上述两种方法之一并缓存该图标以在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.eclipse.swt.program.Program.findProgram(String extension).getImageData(),但我不想使用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.
  • 来自JDIC项目的孵化器的org.jdesktop.jdic.icons.IconService。 IconService的最后一次更改是6年前,在2年前的JDIC上,我找不到可下载的jar。

Is there another solution?

还有其他解决方案吗?

2 个解决方案

#1


4  

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.

FileSystemView在内部使用Win32ShellFolder,因此它们基本相同。

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.

我还挖掘了org.eclipse.swt.program.Program的Source以及org.eclipse.swt.internal.win32.OS。然后OS类使用Icon的本机调用。此时,除非你真的无法创建临时文件,否则我不会走那条路。

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.

对于JDIC,我只找到了http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm并进行了一些挖掘(可能没有相关但是做了很多事情:D) 。也称为原生。

#2


0  

Do you really need the temporary file to use the first option? A File does not have to denote a file that actually exists...

你真的需要临时文件来使用第一个选项吗?文件不必表示实际存在的文件......

#1


4  

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.

FileSystemView在内部使用Win32ShellFolder,因此它们基本相同。

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.

我还挖掘了org.eclipse.swt.program.Program的Source以及org.eclipse.swt.internal.win32.OS。然后OS类使用Icon的本机调用。此时,除非你真的无法创建临时文件,否则我不会走那条路。

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.

对于JDIC,我只找到了http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm并进行了一些挖掘(可能没有相关但是做了很多事情:D) 。也称为原生。

#2


0  

Do you really need the temporary file to use the first option? A File does not have to denote a file that actually exists...

你真的需要临时文件来使用第一个选项吗?文件不必表示实际存在的文件......