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 - 由list.getModel()返回的值getElementAt(index)。
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.