• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableのセルにAnimated GIFを表示する
#navi(../)
RIGHT:Posted by &author(aterai); at 2012-03-05
*JTableのセルにAnimated GIFを表示する [#m7551a70]
ImageIcon にImageObserverを設定して、JTableのセル中でAnimated GIFのアニメーションを行います。
---
title: JTableのセルにAnimated GIFを表示する
tags: [JTable, ImageIcon, ImageObserver]
author: aterai
pubdate: 2012-03-05T01:22:05+09:00
description: ImageIconにImageObserverを設定して、JTableのセル中でAnimated GIFのアニメーションを行います。
---
* 概要 [#m7551a70]
`ImageIcon`に`ImageObserver`を設定して、`JTable`のセル中で`Animated GIF`のアニメーションを行います。

-&jnlp;
-&jar;
-&zip;
#download(https://lh3.googleusercontent.com/-138Snht85-E/T1B6iHoG1pI/AAAAAAAABJw/XaESowuWEC4/s800/AnimatedIconInTableCell.png)

//#screenshot
#ref(https://lh3.googleusercontent.com/-138Snht85-E/T1B6iHoG1pI/AAAAAAAABJw/XaESowuWEC4/s800/AnimatedIconInTableCell.png)

**サンプルコード [#b724b7aa]
* サンプルコード [#b724b7aa]
#code(link){{
ImageIcon icon = new ImageIcon(url);
//Wastefulness: icon.setImageObserver((ImageObserver)table);
icon.setImageObserver(new ImageObserver() {
  //@see http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
  @Override public boolean imageUpdate(
      Image img, int infoflags, int x, int y, int w, int h) {
    //@see javax.swing.JLabel#imageUpdate(...)
    if(!table.isShowing()) return false;
    //@see java.awt.Component#imageUpdate(...)
    if((infoflags & (FRAMEBITS|ALLBITS)) != 0) {
      int vr = table.convertRowIndexToView(row); //JDK 1.6.0
      int vc = table.convertColumnIndexToView(col);
      table.repaint(table.getCellRect(vr, vc, false));
    }
    return (infoflags & (ALLBITS|ABORT)) == 0;
  };
});
}}

**解説 [#b35904a7]
上記のサンプルでは、[http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java]を参考にして、Animated GIFファイルから作成したImageIconに、setImageObserver(ImageObserver)を設定しています。直接JTableをImageObserverとして設定するとすべてのセルが再描画されて無駄なので、 JTable#getCellRect(row, col, false)で対象セルのみrepaintするようにしています。
* 解説 [#b35904a7]
上記のサンプルでは、[http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java]を参考にして、`Animated GIF`ファイルから作成した`ImageIcon`に、`setImageObserver(ImageObserver)`を設定しています。直接`JTable`を`ImageObserver`として設定するとすべてのセルが再描画されて無駄なので、`JTable#getCellRect(row, col, false)`で対象セルのみ`repaint`するようにしています。

- [http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java]からの変更点
-- JTable#isShowing(...)==falseで、非表示の場合はJTable#repaint(...)しない
-- JDK 1.6.0以降に導入されたJTable#convertRowIndexToView(row)を使って、行がソートされていても正しいセルを再描画する
-- JTable#convertColumnIndexToView(col)を使って、列の入れ替えがあっても正しいセルを再描画する
-- `JTable#isShowing(...)==false`で、非表示の場合は`JTable#repaint(...)`しない
-- `JDK 1.6.0`以降に導入された`JTable#convertRowIndexToView(row)`を使って、行がソートされていても正しいセルを再描画する
-- `JTable#convertColumnIndexToView(col)`を使って、列の入れ替えがあっても正しいセルを再描画する

**参考リンク [#e193de0f]
-[http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java]
* 参考リンク [#e193de0f]
- [http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java]
-- 元サイトには繋がらないので、[https://www.google.com/search?q=AnimatedIconTableExample.java animatedicontableexample.java - Google 検索]などのミラーを参考

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