Display file icons from generated file list in swing

I want to do one thing in Swing, I hope I will be clear enough.

I want to display a list of files with icons that the user has associated with this particular file, based on the extension. However, I want this list of files to be generated in the program - I mean: the displayed file icons will not be valid files in the folder (therefore I cannot use JFileChooser ).

Is there anything that can help me?

+4
source share
3 answers

OK It works well. I took this idea and this article .

The idea is that I create a classic JList , but add a custom ListCellRenderer to draw icons that come from temporary files through JFileChooser . The resulting visualizer looks like this (I made the fields static, so they are not recreated every time the JList is executed):

 package app; import java.awt.Component; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.DefaultListCellRenderer; import javax.swing.Icon; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JList; public class PseudofileIconRenderer extends DefaultListCellRenderer { private static HashMap<String, Icon> extIcons = new HashMap<String, Icon>(); private static Pattern p = Pattern.compile("\\.\\w+$"); private static JFileChooser chooser = new JFileChooser(); @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); String filename = (String) value; Matcher m = p.matcher(filename); Icon i; String extension = m.find() ? m.group() : ""; if (extIcons.containsKey(extension)) { i = extIcons.get(extension); } else { File file; try { file = File.createTempFile("icon", extension); file.deleteOnExit(); i = chooser.getIcon(file); extIcons.put(extension, i); } catch (IOException ex) { //this shouldn't happen anyway i = null; } } label.setIcon(i); return label; } 

Then I can populate JList String s, which will represent the files.

+1
source

Hi, I came across this while playing googling ... hop! [alt text] [1] e this helps :)

 import java.io.*; import javax.swing.*; public class IconExtract1 { public static void main(String[] args) throws Exception { String s = "c:/windows/regedit.exe"; File file = new File(s); // Get metadata and create an icon sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file); Icon icon = new ImageIcon(sf.getIcon(true)); System.out.println("type = " + sf.getFolderType()); // show the icon JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT); JFrame frame = new JFrame(); frame.getContentPane().add(ficon); frame.pack(); frame.setVisible(true); } } 

another way:

 import java.io.*; import javax.swing.*; import java.awt.*; import javax.swing.filechooser.FileSystemView; public class IconExtract2 { public static void main(String[] args) throws Exception { String s = "c:/windows/regedit.exe"; File file = new File(s); // Get metadata and create an icon Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file); // show the icon JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT); JFrame frame = new JFrame(); frame.getContentPane().add(ficon); frame.pack(); frame.setVisible(true); } } 

here is the link: http://www.rgagnon.com/javadetails/java-0439.html

+4
source

One ugly hack that I just thought about, I don't know if this will work.

I can create a temporary folder, then put empty files with the same file names, and then introduce JFileChooser in this folder, and then after closing the window, delete this folder.

I would prefer a “cleaner” solution.

+1
source

All Articles