---
title: JFileChooserに画像プレビューを追加
tags: [JFileChooser, ImageIcon, PropertyChangeListener]
author: aterai
pubdate: 2006-11-20
description: JFileChooserに画像のプレビュー機能を追加します。
---
* 概要 [#f95b66b6]
`JFileChooser`に画像のプレビュー機能を追加します。プレビューを表示するコンポーネントは、チュートリアルの[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImagePreview.java ImagePreview.java]をそのまま利用しています。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRN6_UpeI/AAAAAAAAAg0/eJZas5kcb34/s800/PreviewAccessory.png)

* サンプルコード [#a77e7ba1]
#code(link){{
fileChooser = new JFileChooser();
fileChooser.setAccessory(new ImagePreview(fileChooser));
//...

//http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImagePreview.java
class ImagePreview extends JComponent implements PropertyChangeListener {
  private static final int PREVIEW_WIDTH  = 90;
  private static final int PREVIEW_MARGIN = 5;
  private ImageIcon thumbnail = null;
  private File file = null;
  public ImagePreview(JFileChooser fc) {
    setPreferredSize(new Dimension(PREVIEW_WIDTH+PREVIEW_MARGIN*2, 50));
    fc.addPropertyChangeListener(this);
  }
  public void loadImage() {
    if(file == null) {
      thumbnail = null;
      return;
    }
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if(tmpIcon.getIconWidth()>PREVIEW_WIDTH) {
      //Image img = tmpIcon.getImage().getScaledInstance(
      //     PREVIEW_WIDTH,-1,Image.SCALE_DEFAULT);
      //The Perils of Image.getScaledInstance() | Java.net
      //http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
      float scale = PREVIEW_WIDTH/(float)tmpIcon.getIconWidth();
      int newW = (int)(tmpIcon.getIconWidth()  * scale);
      int newH = (int)(tmpIcon.getIconHeight() * scale);
      BufferedImage img = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = (Graphics2D)img.getGraphics();
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                          RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2.drawImage(tmpIcon.getImage(), 0, 0, newW, newH, null);
      g2.dispose();
      thumbnail = new ImageIcon(img);
    }else{
      thumbnail = tmpIcon;
    }
  }
  @Override public void propertyChange(PropertyChangeEvent e) {
    boolean update = false;
    String prop = e.getPropertyName();
    if(JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
      file = null;
      update = true;
    }else if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
      file = (File)e.getNewValue();
      update = true;
    }
    if(update) {
      thumbnail = null;
      if(isShowing()) {
        loadImage();
        repaint();
      }
    }
  }
  @Override protected void paintComponent(Graphics g) {
    if(thumbnail==null) {
      loadImage();
    }
    if(thumbnail!=null) {
      int x = getWidth()/2  - thumbnail.getIconWidth()/2;
      int y = getHeight()/2 - thumbnail.getIconHeight()/2;
      if(y < 0)              y = 0;
      if(x < PREVIEW_MARGIN) x = PREVIEW_MARGIN;
      thumbnail.paintIcon(this, g, x, y);
    }
  }
}
}}

* 解説 [#xdafc59c]
上記のファイルチューザーでは、画像ファイルを選択すると、そのプレビューが表示されます。プレビューコンポーネントは、`JFileChooser#setAccessory(JComponent)`メソッドで、ファイルチューザーに追加しています。

プレビュー側でファイルの選択、解除などのイベントを受け取るために、`PropertyChangeListener`を実装する必要があります。

* 参考リンク [#r0428eb6]
- [http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)]
- [http://www.bekkoame.ne.jp/~bootan/free2.html デジタル出力工房 絵写楽]

* コメント [#fccf3ce2]
#comment
#comment