How can I display an icon for a file from a Java application?

Is it possible from an application on the Java desktop to find an icon to display for a given file, perhaps based on its mime type? In particular, I want to ask the host OS to display the icon so that it can match the expected user.

+3
source share
2 answers

Below is an example here . The relevant code would be:

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

Edit (comment included). The official help page is here .

+8
source

, , , JTree DefaultTreeCellRenderer. Java API.

. - , , , .

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" );
+2

Source: https://habr.com/ru/post/1715081/


All Articles