我想是不是可以把每一项后面加上一个有图标的JLabel,可是又不知道怎么加得上去,请高手指点迷津,要怎么才能够实现 啊
最好详细一点,谢谢!!
------解决方案--------------------------------------------------------
你需要实现ListCellRenderer接口。它是控制combobox弹出的List,并非控制JComboBox的。
下面的代码,你可以参考下:
- Java code
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Font theFont = null; Color theForeground = null; Icon theIcon = null; String theText = null; JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof Object[]) { Object values[] = (Object[]) value; theFont = (Font) values[0]; theForeground = (Color) values[1]; theIcon = (Icon) values[2]; theText = (String) values[3]; } else { theFont = list.getFont(); theForeground = list.getForeground(); theText = ""; } if (!isSelected) { renderer.setForeground(theForeground); } if (theIcon != null) { renderer.setIcon(theIcon); } renderer.setText(theText); renderer.setFont(theFont); return renderer; }