Alex Rivera | Logout

making jfilechooser show image thumbnails

Asked 2010-11-04T12:03:44.390
9

I wanted to create a JFileChooser with thumbnail view of image files.So I subclassed FileView and in the method which creates ImageIcon did some scaling sothat thumbnail images are shown.

However,the overall effect is that, the filechooser widget takes some time before opening a directory and showing thumbnails..In createImageIcon() below,I need to call new ImageIcon() twice-once with the image filepath and next with the resized image as constructor argument.I think this is what slows the widget .

Is there a more efficient alternative?Any suggestions/pointers most welcome.

thanks, mark

public static void main(String[] args) { 
    JFileChooser chooser=new JFileChooser();
    ThumbNailView thumbView=new ThumbNailView();
    chooser.setFileView(thumbView);
  }

class ThumbNailView extends FileView{
 public Icon getIcon(File f){
  Icon icon=null;
  if(isImageFile(f.getPath())){
   icon=createImageIcon(f.getPath(),null);
  }
  return icon;
 }
 private ImageIcon createImageIcon(String path,String description) {
  if (path != null) {
   ImageIcon icon=new ImageIcon(path);
   Image img = icon.getImage() ; 
   Image newimg = img.getScaledInstance( 16, 16,  java.awt.Image.SCALE_SMOOTH ) ;
   return new ImageIcon(newimg);
  } else {
   System.err.println("Couldn't find file: " + path);
   return null;
   }
}

private boolean isImageFile(String filename){
    //return true if this is image
}
Edit
Report

1 Answer

8

Use fileDialog instead of JfileChooser for choising the image:

FileDialog fd = new FileDialog(frame, "Test", FileDialog.LOAD);
String Image_path

fd.setVisible(true);
name = fd.getDirectory() + fd.getFile();
        image_path=name;
        ImageIcon icon= new ImageIcon(name);
        icon.setImage(icon.getImage().getScaledInstance(jLabel2.getWidth(),jLabel2.getHeight() , Image.SCALE_DEFAULT));
        jLabel2.setIcon(icon);
answered 2012-11-10T17:27:22.713

Your Answer