TITLE:JListのセルのアニメーション
#navi(../)
*JListのセルのアニメーション [#gfab1816]
Posted by [[terai]] at 2006-11-27

#contents

**概要 [#db3c3494]
JListの選択されたセルをアニメーションさせます。

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

#screenshot

**サンプルコード [#dddb9e20]
#code{{
class AnimeListCellRenderer extends JPanel implements ListCellRenderer, ActionListener {
  private static final Color eColor = new Color(240,240,255);
  private static final Color sColor = new Color(230,230,255);
  private final AnimeIcon icon = new AnimeIcon();
  private final ScrollingLabel label = new ScrollingLabel();
  private final javax.swing.Timer animator;
  private final JList jlist;
  private boolean isRunning = false;
  public AnimeListCellRenderer(JList list) {
    super(new BorderLayout());
    jlist = list;
    animator = new javax.swing.Timer(100, this);
    animator.start();
    setOpaque(true);
    add(icon,  BorderLayout.WEST);
    add(label, BorderLayout.CENTER);
  }
  public Component getListCellRendererComponent(JList list, Object object,
          int index, boolean isSelected, boolean cellHasFocus) {
    isRunning = isSelected;
    setBackground(isSelected?sColor:index%2==0?eColor:list.getBackground());
    label.setText((object==null) ? "" : object.toString());
    return this;
  }
  public void actionPerformed(ActionEvent e) {
    jlist.repaint();
  }
  private class ScrollingLabel extends JLabel {
    public void ScrollingLabel() {
      setOpaque(false);
    }
    public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
.....
}}

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

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

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

**コメント [#x428be3b]
#comment