您必須實現ListCellRenderer(或延長DefaultListCellRenderer),並有getListCellRendererComponent
方法與它的圖標返回Jlabel
。
例子:
public class IconListRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
Icon icon = this.getIcon(list, value, index, isSelected, cellHasFocus)
label.setIcon(icon);
return label;
}
protected Icon getIcon(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
// how do I get icon?
}
}
您必須實現getIcon
方法。
接受一個答案,從而人們可以知道哪個解決方案解決了您的問題。 – prolink007 2011-08-26 01:33:41
請接受答案!標記問題左側的'V',它會變成綠色,這意味着這是適合你的解決方案。如果您對答案不滿意,請發表評論,說明缺少的內容可以被您接受。 – Math 2013-10-11 13:32:33