Swing/AnimatedIconInTableCell のバックアップ(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
- 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:
Summary
ImageIcon
にImageObserver
を設定して、JTable
のセル中でAnimated GIF
のアニメーションを行います。
Screenshot

Advertisement
Source Code Examples
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, KotlinExplanation
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 検索などのミラーを参考
- JTreeのTreeNodeにAnimated GIFを表示する