Swing/ListCellAnimation のバックアップの現在との差分(No.5)
- category: swing folder: ListCellAnimation title: JListのセルのアニメーション tags: [JList, ListCellRenderer, Animation] author: aterai pubdate: 2006-11-27 pubdate: 2006-11-27T14:03:02+09:00 description: JListの選択されたセルをアニメーションさせます。 image:
概要
概要
JList
の選択されたセルをアニメーションさせます。
Screenshot
Advertisement
サンプルコード
サンプルコード
class AnimeListCellRenderer extends JPanel implements ListCellRenderer {
private static final Color selectedColor = new Color(230,230,255);
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;
int animate_index = -1;
#spanadd
#spanend
public AnimeListCellRenderer(final JList l) {
super(new BorderLayout());
this.list = l;
animator = new Timer(80, new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
int i = l.getSelectedIndex();
if(isRunning=(i>=0)) l.repaint(l.getCellBounds(i,i));
if (isRunning = (i >= 0)) {
l.repaint(l.getCellBounds(i, i));
}
}
});
setOpaque(true);
add(icon, BorderLayout.WEST);
add(label);
animator.start();
}
#spanadd
#spanend
@Override public Component getListCellRendererComponent(JList list, Object object,
int index, boolean isSelected, boolean cellHasFocus) {
int index, boolean isSelected, boolean cellHasFocus) {
setBackground(isSelected ? selectedColor : list.getBackground());
label.setText((object==null) ? "" : object.toString());
label.setText((object == null) ? "" : object.toString());
animate_index = index;
return this;
}
#spanadd
#spanend
private boolean isAnimatingCell() {
return isRunning && animate_index==list.getSelectedIndex();
return isRunning && animate_index == list.getSelectedIndex();
}
int animate_index = -1;
#spanadd
#spanend
private class MarqueeLabel extends JLabel {
//...
// ...
}
// ...
#spanadd
}
#spanend
View in GitHub: Java, Kotlin解説
上記のサンプルでは、セルが選択されると左のアイコンがアニメーションし、文字列がクリップされている場合は、スクロールするようになっています。解説
上記のサンプルでは、セルが選択されると左のアイコンがアニメーションし、文字列が省略されている場合はスクロールするよう設定しています。選択されたセルだけ再描画しているのではなく、
選択されたセルだけ再描画してアニメーションを行っています。ActionListener
を実装したセルレンダラーを作成してJList
全体をrepaint
しています。