TITLE:JFileChooserに画像プレビューを追加

JFileChooserに画像プレビューを追加

編集者:Terai Atsuhiro~

作成日:2006-11-20
更新日:2023-06-30 (金) 10:43:06
  • category: swing folder: PreviewAccessory title: JFileChooserに画像プレビューを追加 tags: [JFileChooser, ImageIcon, PropertyChangeListener] author: aterai pubdate: 2006-11-20T10:35:42+09:00 description: JFileChooserに選択した画像ファイルのプレビューを表示する機能を追加します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRN6_UpeI/AAAAAAAAAg0/eJZas5kcb34/s800/PreviewAccessory.png

概要

JFileChooserに選択した画像ファイルのプレビューを表示する機能を追加します。プレビューを表示するコンポーネントは、チュートリアルのImagePreview.javaをそのまま利用しています。

概要

JFileChooserに画像のプレビュー機能を追加します。

サンプルコード

#spanend
#spanadd
fileChooser = new JFileChooser();
#spanend
#spanadd
fileChooser.setAccessory(new ImagePreview(fileChooser));
#spanend
#spanadd
// ...
#spanend

#spandel
プレビューを表示するコンポーネントは、チュートリアルの[[ImagePreview.java>http://java.sun.com/docs/books/tutorial/uiswing/components/examples/ImagePreview.java]]をそのまま利用しています。
#spanend
#spanadd
class ImagePreview extends JComponent implements PropertyChangeListener {
#spanend
  private static final int PREVIEW_WIDTH = 90;
  private static final int PREVIEW_MARGIN = 5;
  private ImageIcon thumbnail;
  private File file;

#spandel
#screenshot
#spanend
  public ImagePreview(JFileChooser fc) {
    super();
    setPreferredSize(new Dimension(PREVIEW_WIDTH + PREVIEW_MARGIN * 2, 50));
    fc.addPropertyChangeListener(this);
  }

#spandel
**サンプルコード [#a77e7ba1]
#spanend
 fileChooser = new JFileChooser();
 fileChooser.setAccessory(new ImagePreview(fileChooser));
  private void loadImage() {
    if (file == null) {
      thumbnail = null;
      return;
    }
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if (tmpIcon.getIconWidth() > PREVIEW_WIDTH) {
      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 = img.createGraphics();
      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;
    }
  }

 //http://java.sun.com/docs/books/tutorial/uiswing/components/examples/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 == null) return;
     if(tmpIcon.getIconWidth()>PREVIEW_WIDTH) {
       Image img = tmpIcon.getImage().getScaledInstance(PREVIEW_WIDTH,-1,Image.SCALE_DEFAULT);
       thumbnail = new ImageIcon(img);
     }else{
       thumbnail = tmpIcon;
     }
   }
   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();
       }
     }
   }
   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);
     }
   }
 }
  @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();
      }
    }
  }

-&jnlp;
-&jar;
-&zip;
  @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);
    }
  }
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin

解説

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

解説

  • JFileChooserのファイルリストで画像ファイルを選択すると、そのプレビューをJFileChooser内に表示する
  • プレビュー表示に使用するコンポーネント(JLabel)はJFileChooser#setAccessory(JComponent)メソッドで追加
  • プレビューコンポーネントはPropertyChangeListenerを実装してファイルの選択、解除などのイベントを受け取る
プレビュー側でファイルの選択、解除などのイベントを受け取るために、PropertyChangeListener を実装する必要があります。

参考リンク

参考リンク

コメント

コメント