Add Image Preview Pane to JFileChooser

Screenshot of Image Preview JFileChooser
Let me show you how to add image preview pane to JFileChooser. This is very simple, you might be familiar with the code of showing an image in swing and adding custom component to JFileChooser. Let us combine these two now to add an Image Preview pane of the selected file in JFileChooser.

import javax.swing.*;
import java.awt.*;
import java.beans.*;
import javax.swing.filechooser.*;
import java.awt.image.*;
import javax.imageio.*;
import java.util.concurrent.*;
import java.awt.event.*;
class ImagePreviewJFileChooser extends JFrame
JLabel img;
JButton open;
JFileChooser jf=new JFileChooser();

    public ImagePreviewJFileChooser()
    private void createAndShowGUI()
        setTitle("Image Preview for JFileChooser");
        setLayout(new FlowLayout());
        // Create label
        img=new JLabel();
        // Let label come fatty!!
        img.setPreferredSize(new Dimension(175,175));
        // Set label as accessory
        // Accept only image files
        // Create filter for image files
        FileNameExtensionFilter filter=new FileNameExtensionFilter("Image Files","jpg","jpeg","png","gif");
        // Set it as current filter

        // Add property change listener
        jf.addPropertyChangeListener(new PropertyChangeListener(){
            // When any JFileChooser property changes, this handler
            // is executed
            public void propertyChange(final PropertyChangeEvent pe)
                // Create SwingWorker for smooth experience
                SwingWorker<Image,Void> worker=new SwingWorker<Image,Void>(){
                    // The image processing method
                    protected Image doInBackground()
                        // If selected file changes..
                        // Get selected file
                        File f=jf.getSelectedFile();
                            // Create FileInputStream for file
                            FileInputStream fin=new FileInputStream(f);
                            // Read image from fin
                            // Return the scaled version of image
                            return bim.getScaledInstance(178,170,BufferedImage.SCALE_FAST);
                            }catch(Exception e){
                                // If there is a problem reading image,
                                // it might not be a valid image or unable
                                // to read
                                img.setText(" Not valid image/Unable to read");
                    return null;
                    protected void done()
                        // Get the image
                        Image i=get(1L,TimeUnit.NANOSECONDS);
                        // If i is null, go back!
                        if(i==null) return;
                        // Set icon otherwise
                        img.setIcon(new ImageIcon(i));
                        }catch(Exception e){
                            // Print error occured
                            img.setText(" Error occured.");
                // Start worker thread
        // Create JButton
        open=new JButton("Open File Chooser");
        open.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
                // Show open dialog
    public static void main(String args[])
        new ImagePreviewJFileChooser();

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

Posted by at on

Tags: Swing Hacks,