E
-价值观类型这个渲染器可以用于
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;
// check if this cell represents the current DnD drop location
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsert()
&& dropLocation.getIndex() == index) {
background = Color.BLUE;
foreground = Color.WHITE;
// check if this cell is selected
} else if (isSelected) {
background = Color.RED;
foreground = Color.WHITE;
// unselected, and not the DnD drop location
} else {
background = Color.WHITE;
foreground = Color.BLACK;
};
setBackground(background);
setForeground(foreground);
return this;
}
}
JList
,
DefaultListCellRenderer
Component getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus)
paint
方法来“渲染”的细胞。如果要计算一个列表的尺寸因为列表细胞没有固定的大小,这种方法被称为生成一个组件上,
getPreferredSize
可以调用。
list
JList我们画的。
value
列表返回的值。getmodel() getelementat(指数)。
index
-细胞指数。
isSelected
-如果选择指定的单元格。
cellHasFocus
-如果指定的细胞具有焦点。
JList
,
ListSelectionModel
,
ListModel
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.