Swing/AnimatedTreeNode のバックアップ(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/AnimatedTreeNode へ行く。
- category: swing
folder: AnimatedTreeNode
title: JTreeのTreeNodeにAnimated GIFを表示する
tags: [JTree, ImageIcon, ImageObserver, Animation]
author: aterai
pubdate: 2016-06-27T01:54:58+09:00
description: JTreeのノードに設定したAnimated GIFのImageIconがアニメーションを行えるようにImageObserverを設定します。
image:
Summary
JTree
のノードに設定したAnimated GIF
のImageIcon
がアニメーションを行えるようにImageObserver
を設定します。
Screenshot

Advertisement
サンプルコード
TreePath path = new TreePath(s1.getPath());
icon.setImageObserver((img, infoflags, x, y, w, h) -> {
if (!tree.isShowing()) {
return false;
}
Rectangle cellRect = tree.getPathBounds(path);
if ((infoflags & (FRAMEBITS | ALLBITS)) != 0 && Objects.nonNull(cellRect)) {
tree.repaint(cellRect);
}
return (infoflags & (ALLBITS | ABORT)) == 0;
});
View in GitHub: Java, Kotlin解説
Default
DefaultMutableTreeNode
のUserObject
としてAnimated GIF
画像から生成したImageIcon
を設定し、これをDefaultTreeCellRenderer#setIcon(...)
でノードに表示- 自動的に再描画されないためアニメーションが開始されない
setImageObserver
- 同様に設定した
ImageIcon
にsetImageObserver(ImageObserver)
メソッドでImageObserver
を追加し、正常にアニメーションが行えるように画像の更新に合わせてJTree#repaint(Rectangle)
でノードを再描画 - 再描画する領域は対象のノードのみになるよう
JTree.getPathBounds(TreePath)
で取得される領域に制限
- 同様に設定した