1

I'm trying to get the Thumbnail that is associated to a particular file, and then resize it. I've been testing on Mac, and haven't been able to find a solution that would allow me to achieve this.

Code so far:

import com.apple.laf.AquaIcon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;


public class TestSystemIcon extends JFrame
{

 JPanel panel;
 ImageIcon icon;
 public TestSystemIcon()
 {

     panel = new JPanel();
     JButton button = new JButton("Open...");
     final JLabel label = new JLabel();
     icon = null;
     final JPanel en = new JPanel(new FlowLayout(FlowLayout.CENTER));
     label.setHorizontalAlignment(SwingConstants.CENTER);
     button.addActionListener(new ActionListener()
     {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JFileChooser fileChooser = new JFileChooser();
            if(fileChooser.showOpenDialog(null) == JFileChooser.OPEN_DIALOG)
            {
                File file = fileChooser.getSelectedFile();
                icon = resizeIcon(getThumbnail1(file),200,200);
//                    icon = resizeIcon(getThumbnail2(file),200,200);
                System.out.println(icon);
                label.setIcon(icon);
                en.add(label);
                revalidate();
                repaint();

            }
        }
    });

    panel.add(button);
    this.add(panel,BorderLayout.NORTH);
    this.add(en,BorderLayout.CENTER);

    this.setSize(400,400);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 }


 public ImageIcon getThumbnail1(File file)
 {
    JFileChooser f = new JFileChooser();
    Icon i = f.getIcon(file);
    //Mac Conversion.
    Image image = AquaIcon.getImageForIcon(i);
    return new ImageIcon(image);
 }

public ImageIcon getThumbnail2(File file)
{
    return new ImageIcon(file.getPath());
}

 public ImageIcon resizeIcon(ImageIcon imageIcon,int width, int height)
 {
    return new ImageIcon(imageIcon.getImage().getScaledInstance(width,height,Image.SCALE_SMOOTH));
 }


 public static void main(String[] args)
 {
     TestSystemIcon test = new TestSystemIcon();
     test.setVisible(true);
 }
}

Version1 of getting the thumbnail has following behaviour:

  • Can open thumbnails
  • Very small, scaling not suitable.

Version2 of getting thumbnail has following behaviour:

  • Doesn't display image, despite finding image (System.out proves this). Except for pdf, where instead it displays the actual file, as opposed to the thumbnail
  • When it does work i.e. pdf, it scales nicely.

I know I can use sun.awt.shell.ShellFolder;, however I am aiming for a cross-platform solution.

Thanks for any help

Ell_18
  • 71
  • 2
  • 9

1 Answers1

1

I looked at some code that I have done in the past and seems this works fine when you use JLabel with and ImageIcon, try this code that resized a large image to 100x100,

 ImageIcon icon = new ImageIcon("Penguins.jpg");
 Image img = icon.getImage().getScaledInstance(100,100,Image.SCALE_SMOOTH);

 // if the file is not an image, but a file on the system,
 Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
 Image img = ((ImageIcon) icon).getImage().getScaledInstance(100,100,Image.SCALE_SMOOTH);

 ImageIcon icon1 = new ImageIcon(img);
 JLabel image  = new JLabel(icon1);
faljbour
  • 1,367
  • 2
  • 8
  • 15
  • Thanks for your reply, however the aim is to get the thumbnail of any file, as opposed to an image. i.e. If I opened a docx document, I would get the Microsoft Word thumbnail icon – Ell_18 Feb 18 '15 at 08:21
  • @EII_18, I found a duplicate post that addresses the icon issue, but not re-sizing of the image issue, look at http://stackoverflow.com/questions/1498506/java-howto-get-the-thumbnail-from-a-file I tried the logic it works, but if you need to reszie the icon use the logic in my answer – faljbour Feb 18 '15 at 21:09
  • I updated the answer to show the info provided from the 1498506 post. – faljbour Feb 18 '15 at 21:19