|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
public interface ListCellRenderer
标识可用作“橡皮图章”以绘制 JList 中单元格的组件。例如,要将 JLabel 用作 ListCellRenderer,则需要编写:
class MyCellRenderer extends JLabel implements ListCellRenderer {
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 list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
返回已配置用于显示指定值的组件。 |
| 方法详细信息 |
|---|
Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
paint 方法来“呈现”单元格。如果由于列表单元格没有固定的大小而有必要计算该列表的尺寸,则调用此方法来生成一个可在其上调用 getPreferredSize 的组件。
list - 正在绘制的 JList。value - 由 list.getModel().getElementAt(index) 返回的值。index - 单元格索引。isSelected - 如果选择了指定的单元格,则为 true。cellHasFocus - 如果指定的单元格拥有焦点,则为 true。
JList,
ListSelectionModel,
ListModel
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。