• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JListのセルのアニメーション
#navi(../)
*JListのセルのアニメーション [#gfab1816]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-11-27~
更新日:&lastmod;
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 flag = 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) {
     flag = isSelected;
     setBackground(flag?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 {
     private float xx;
     public void ScrollingLabel() {
       setOpaque(false);
     }
     public void paintComponent(Graphics g) {
       Graphics2D g2d = (Graphics2D) g;
 ......
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;
.....
}}
-&jnlp;
-&jar;
-&zip;

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

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

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

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