Swing/AnimatedIconInTableCell のバックアップ(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/AnimatedIconInTableCell へ行く。
- 1 (2012-03-05 (月) 01:22:05)
- 2 (2012-03-05 (月) 15:42:20)
- 3 (2012-12-13 (木) 15:22:42)
- 4 (2014-12-12 (金) 15:17:01)
- 5 (2015-03-05 (木) 00:31:51)
- 6 (2016-06-27 (月) 01:56:51)
- 7 (2017-09-24 (日) 15:15:33)
- 8 (2019-03-14 (木) 13:45:54)
- 9 (2020-12-22 (火) 15:46:37)
- 10 (2023-05-30 (火) 22:15:12)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
TITLE:JTableのセルにAnimated GIFを表示する
Posted by aterai at 2012-03-05
JTableのセルにAnimated GIFを表示する
ImageIcon にImageObserverを設定して、JTableのセル中でAnimated GIFのアニメーションを行います。
- &jnlp;
- &jar;
- &zip;
サンプルコード
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;
};
});
View in GitHub: Java, Kotlin解説
上記のサンプルでは、AnimatedIconTableExample.javaを参考にして、Animated GIFファイルから作成したImageIconに、setImageObserver(ImageObserver)を設定しています。直接JTableをImageObserverとして設定するとすべてのセルが再描画されて無駄なので、 JTable#getCellRect(row, col, false)で対象セルのみrepaintするようにしています。
- AnimatedIconTableExample.javaからの変更点
- JTable#isShowing(...)==falseで、非表示の場合はJTable#repaint(...)しない
- JDK 1.6.0以降に導入されたJTable#convertRowIndexToView(row)を使って、行がソートされていても正しいセルを再描画する
- JTable#convertColumnIndexToView(col)を使って、列の入れ替えがあっても正しいセルを再描画する
参考リンク
- AnimatedIconTableExample.java
- 元サイトには繋がらないので、animatedicontableexample.java - Google 検索などのミラーを参考