• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JListのセルのアニメーション
#navi(../)
*JListのセルのアニメーション [#gfab1816]
Posted by [[terai]] at 2006-11-27
#tags(JList, ListCellRenderer, Animation)
RIGHT:Posted by &author(aterai); at 2006-11-27
* JListのセルのアニメーション [#gfab1816]
``JList``の選択されたセルをアニメーションさせます。

#contents
- &jnlp;
- &jar;
- &zip;

**概要 [#db3c3494]
JListの選択されたセルをアニメーションさせます。
#ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPa7B8VkI/AAAAAAAAAd8/uLpJ50Oxwf8/s800/ListCellAnimation.png)

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#dddb9e20]
#code{{
** サンプルコード [#dddb9e20]
#code(link){{
class AnimeListCellRenderer extends JPanel implements ListCellRenderer {
  private static final Color selectedColor = new Color(230,230,255);
  private final AnimeIcon icon = new AnimeIcon();
  private final MarqueeLabel label = new MarqueeLabel();
  private final javax.swing.Timer animator;
  private final JList list;
  private boolean isRunning = false;
  public AnimeListCellRenderer(final JList l) {
    super(new BorderLayout());
    this.list = l;
    animator = new javax.swing.Timer(80, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int i = l.getSelectedIndex();
        if(isRunning=(i>=0)) l.repaint(l.getCellBounds(i,i));
      }
    });
    setOpaque(true);
    add(icon,  BorderLayout.WEST);
    add(label, BorderLayout.CENTER);
    add(label);
    animator.start();
  }
  public Component getListCellRendererComponent(JList list, Object object,
            int index, boolean isSelected, boolean cellHasFocus) {
    setBackground(isSelected ? selectedColor : list.getBackground());
    label.setText((object==null) ? "" : object.toString());
    animate_index = index;
    return this;
  }
  private boolean isAnimatingCell() {
    return isRunning && animate_index==list.getSelectedIndex();
  }
  int animate_index = -1;
  private class MarqueeLabel extends JLabel {
  //...
}}

**解説 [#u20e47f0]
** 解説 [#u20e47f0]
上記のサンプルでは、セルが選択されると左のアイコンがアニメーションし、文字列がクリップされている場合は、スクロールするようになっています。

%%選択されたセルだけ再描画しているのではなく、ActionListener を実装したセルレンダラーを作成してJList全体をrepaintしています。%%
%%選択されたセルだけ再描画しているのではなく、``ActionListener``を実装したセルレンダラーを作成して``JList``全体を``repaint``しています。%%
選択されたセルだけ再描画してアニメーションを行っています。

**参考リンク [#e65685cf]
-[[Timerでアニメーションするアイコンを作成>Swing/AnimeIcon]]
-[[GlyphVectorで文字列を電光掲示板風にスクロール>Swing/ScrollingMessage]]
** 参考リンク [#e65685cf]
- [[Timerでアニメーションするアイコンを作成>Swing/AnimeIcon]]
- [[GlyphVectorで文字列を電光掲示板風にスクロール>Swing/ScrollingMessage]]

**コメント [#x428be3b]
- 選択されたセルのみ再描画、JScrollPaneに対応、スクリーンショット更新。 -- [[terai]] &new{2008-05-13 (火) 14:53:51};
** コメント [#x428be3b]
- 選択されたセルのみ再描画、``JScrollPane``に対応、スクリーンショット更新。 -- [[aterai]] &new{2008-05-13 (火) 14:53:51};

#comment