如何在不创建临时文件的情况下获取文件扩展名或文件类型的图标? [英] How to get the icon for a file extension or filetype without creating a temp file?

查看:177
本文介绍了如何在不创建临时文件的情况下获取文件扩展名或文件类型的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JTable 中列出了一些带有图标的文件名(如Windows资源管理器中的那些)。如果我从本地文件系统获得 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 )

表示16x16图标或较大图标:

for a 16x16 icon or for a bigger one:

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

由于我的文件存储在数据库中,我没有文件对象。我的解决方法是创建具有特定文件扩展名的临时文件,使用上述两种方法之一并缓存该图标以在CellRenderer中显示它。

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(),但我不想使用SWT

  • org.jdesktop.jdic.icons.IconService 来自 JDIC 项目。 IconService 的最后一次更改是6年前,在2年前的JDIC上,我找不到可下载的jar。

  • 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.

还有其他解决方案吗?

推荐答案

看起来像你已经发现了这样做的方式,除非你想深入了解本地库等。

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

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

FileSystemView uses Win32ShellFolder internally so they are basically the same.

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

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.

对于JDIC我只发现 http://kickjava.com/src/org/jdesktop/jdic/tray/internal/impl/WinTrayIconService.java.htm 进行一些挖掘(可能不是相关但有愚蠢的事情:D)。也称为原生。

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.

这篇关于如何在不创建临时文件的情况下获取文件扩展名或文件类型的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆