简体   繁体   English

如何从Java应用程序中获取文件显示的图标?

[英]How can I get the icon to display for a file from a Java app?

Is it possible from a Java desktop app to find the icon to display for a given file, perhaps based on it's mime type? 是否可以从Java桌面应用程序中找到要为给定文件显示的图标,可能是基于它的mime类型? Specifically I want to ask the host OS for the icon to display so it can match what the user would expect to see. 具体来说,我想要求主机操作系统显示图标,以便它可以匹配用户期望看到的内容。

There is some example here . 有一些例子在这里 The relevant code would be: 相关代码将是:

FileSystemView view = FileSystemView.getFileSystemView();    
Icon icon = view.getSystemIcon(file);    

Edit (included comment) The official help page is here . 编辑 (包含注释)官方帮助页面在这里

AS far as I know the only thing you can do is using the JTree's DefaultTreeCellRenderer. 据我所知,你唯一能做的就是使用JTree的DefaultTreeCellRenderer。 You can read more in the Java API . 您可以在Java API中阅读更多内容。

I'll give you a few examples here. 我在这里举几个例子。 I haven't used it in a while so you will need to dig a little deeper to get what you want. 我有一段时间没有使用它,所以你需要深入挖掘才能得到你想要的东西。

UIDefaults defaults = UIManager.getDefaults( );

Icon computerIcon = defaults.getIcon( "FileView.computerIcon" );
Icon floppyIcon   = defaults.getIcon( "FileView.floppyDriveIcon" );
Icon diskIcon     = defaults.getIcon( "FileView.hardDriveIcon" );
Icon fileIcon     = defaults.getIcon( "FileView.fileIcon" );
Icon folderIcon   = defaults.getIcon( "FileView.directoryIcon" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM