高手请指教,在JComboBox 列表的每一项后面加上小图标,点击小图标,删除这一项 我想是不是可以把每一项后面加上一个有图标的JLabel,可是又不知道怎么加得上去,请高手指点迷津,要怎么才能够实现 啊最好详细一点,谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你需要实现ListCellRenderer接口。它是控制combobox弹出的List,并非控制JComboBox的。下面的代码,你可以参考下: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; } java中如何是JTextArea组件的大小随窗口的增大,而改变 mp3文件合成 求关于字符串的优秀算法....欢迎参与! 组成数组的问题 Java是平台,还是编程语言,或者两者兼备??? JFrame 标题栏上,,怎么设置只看见一个关闭按钮 求林业GIS管理系统软件 这样的变量如何设置?(在线等待) **在线等待**怎样实现鼠标带提示信息 初学java,请各位给我推荐比较好的java书 连接Oracle数据库插入数据却总是报错,非常苦恼,求大侠解释 泛型的疑惑。。
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;
}