|
JavaTM 2 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 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。