Swing/AnimatedIconInTableCell のバックアップソース(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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のアニメーションを行います。 -&jnlp; -&jar; -&zip; //#screenshot #ref(https://lh3.googleusercontent.com/-138Snht85-E/T1B6iHoG1pI/AAAAAAAABJw/XaESowuWEC4/s800/AnimatedIconInTableCell.png) **サンプルコード [#b724b7aa] #code{{ 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するようにしています。 - [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)を使って、列の入れ替えがあっても正しいセルを再描画する **参考リンク [#e193de0f] -[http://www2.gol.com/users/tame/swing/examples/SwingExamples.html AnimatedIconTableExample.java] **コメント [#d8bcbaea] #comment