您必须实现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