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