ListCellRenderer
ListCellRenderer (JList 셀을 그리는 컴포넌트)
JList의 셀을 그리기 위해 "고무 도장(rubber stamps)"으로 사용할 수 있는 컴포넌트를 식별합니다.
본문
ListCellRenderer는 JList의 셀을 그리기 위해 "고무 도장"으로 사용할 수 있는 컴포넌트를 식별합니다. 예를 들어 JLabel을 ListCellRenderer로 사용하려면 다음과 같이 작성합니다.
public interface ListCellRenderer<E>
class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
public MyCellRenderer() { setOpaque(true); }
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
Color background; Color foreground;
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null && !dropLocation.isInsert() &&
dropLocation.getIndex() == index) {
background = Color.BLUE; foreground = Color.WHITE;
} else if (isSelected) {
background = Color.RED; foreground = Color.WHITE;
} else {
background = Color.WHITE; foreground = Color.BLACK;
}
setBackground(background); setForeground(foreground);
return this;
}
}