Swing/AnimatedIconInTableCell のバックアップの現在との差分(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/AnimatedIconInTableCell へ行く。
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableのセルにAnimated GIFを表示する #navi(../) RIGHT:Posted by [[aterai]] at 2012-03-05 *JTableのセルにAnimated GIFを表示する [#m7551a70] ImageIcon にImageObserverを設定して、JTableのセル中でAnimated GIFのアニメーションを行います。 --- category: swing folder: AnimatedIconInTableCell title: JTableのセルにAnimated GIFを表示する tags: [JTable, ImageIcon, ImageObserver, Animation] author: aterai pubdate: 2012-03-05T01:22:05+09:00 description: ImageIconにImageObserverを設定して、JTableのセル中でAnimated GIFのアニメーションを行います。 image: https://lh3.googleusercontent.com/-138Snht85-E/T1B6iHoG1pI/AAAAAAAABJw/XaESowuWEC4/s800/AnimatedIconInTableCell.png --- * 概要 [#summary] `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] #code{{ * サンプルコード [#sourcecode] #code(link){{ ImageIcon icon = new ImageIcon(url); //Wastefulness: icon.setImageObserver((ImageObserver)table); // Wastefulness: icon.setImageObserver((ImageObserver) table); icon.setImageObserver(new ImageObserver() { //@see http://www2.gol.com/users/tame/swing/examples/SwingExamples.html // @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 // @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; 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するようにしています。 - [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)を使って、列の入れ替えがあっても正しいセルを再描画する * 解説 [#explanation] - %%[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`以降に導入された[https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#convertRowIndexToView-int- JTable#convertRowIndexToView(row)]メソッドを使用し、行がソートされていても正しいセルのみを再描画する -- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#convertColumnIndexToView-int- JTable#convertColumnIndexToView(col)]メソッドを使って、列の入れ替えがあっても正しいセルのみを再描画する **参考リンク [#e193de0f] -[http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java] * 参考リンク [#reference] - %%[http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java]%% -- 元サイトには繋がらないので、[https://www.google.com/search?q=AnimatedIconTableExample.java animatedicontableexample.java - Google 検索]などのミラーを参考 - [[JTreeのTreeNodeにAnimated GIFを表示する>Swing/AnimatedTreeNode]] **コメント [#d8bcbaea] * コメント [#comment] #comment #comment