Swing/PreviewAccessory のバックアップソース(No.17)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/PreviewAccessory へ行く。
- 1 (2006-11-21 (火) 06:39:23)
- 2 (2007-05-29 (火) 19:41:03)
- 3 (2007-10-24 (水) 00:32:58)
- 4 (2010-03-08 (月) 13:03:29)
- 5 (2010-11-21 (日) 00:25:37)
- 6 (2010-11-22 (月) 14:38:08)
- 7 (2011-05-08 (日) 23:31:12)
- 8 (2012-07-26 (木) 19:27:53)
- 9 (2014-12-03 (水) 15:52:49)
- 10 (2015-01-23 (金) 19:21:32)
- 11 (2015-03-18 (水) 18:49:31)
- 12 (2016-09-06 (火) 13:42:08)
- 13 (2017-04-04 (火) 14:17:08)
- 14 (2017-08-14 (月) 15:39:56)
- 15 (2017-09-28 (木) 13:55:00)
- 16 (2019-03-28 (木) 14:43:33)
- 17 (2019-05-22 (水) 19:35:38)
- 18 (2021-01-05 (火) 14:10:43)
- 19 (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 --- * 概要 [#summary] `JFileChooser`に選択した画像ファイルのプレビューを表示する機能を追加します。プレビューを表示するコンポーネントは、チュートリアルの[https://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) * サンプルコード [#sourcecode] #code(link){{ fileChooser = new JFileChooser(); fileChooser.setAccessory(new ImagePreview(fileChooser)); //... //https://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; private File file; public ImagePreview(JFileChooser fc) { super(); setPreferredSize(new Dimension(PREVIEW_WIDTH + PREVIEW_MARGIN * 2, 50)); fc.addPropertyChangeListener(this); //setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, SystemColor.inactiveCaption)); } private 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 //ttp://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html //The Perils of Image.getScaledInstance() Blog | Oracle Community //https://community.oracle.com/docs/DOC-983611 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; } } @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); } } } }} * 解説 [#explanation] - `JFileChooser`のファイルリストで画像ファイルを選択すると、そのプレビューを`JFileChooser`内に表示する - プレビュー表示に使用するコンポーネント(`JLabel`)は`JFileChooser#setAccessory(JComponent)`メソッドで追加 - プレビューコンポーネントは`PropertyChangeListener`を実装してファイルの選択、解除などのイベントを受け取る * 参考リンク [#reference] - [https://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)] - [https://community.oracle.com/docs/DOC-983611 The Perils of Image.getScaledInstance() Blog | Oracle Community] - [http://www.bekkoame.ne.jp/~bootan/free2.html デジタル出力工房 絵写楽] - [[MetalLookAndFeelでJFileChooserの下部にコンポーネントを追加する>Swing/FileChooserBottomAccessory]] * コメント [#comment] #comment #comment