创建头像下拉列表
2012-04-25 12:19
176 查看
HashMap<String,ImageIcon> getIcons() { //返回一个Map, key是文件的路径 File folder = new File("images/heads"); File[] files = folder.listFiles(); HashMap<String, ImageIcon> heads = new HashMap<String, ImageIcon>(); for (int i = 0; i < files.length; i++) { File file = files[i]; heads.put(file.getPath(), new ImageIcon(file.getAbsolutePath())); } return heads; }
this.headSelect = new JComboBox(this.heads.keySet().toArray()); this.headSelect.setMaximumRowCount(5); this.headSelect.setRenderer(new HeadComboBoxRenderer(this.heads));然后添加
HeadComboBoxRenderer类
import java.awt.Component; import java.util.HashMap; import java.util.Map; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; public class HeadComboBoxRenderer extends JLabel implements ListCellRenderer { private Map<String, ImageIcon> heads; public HeadComboBoxRenderer(HashMap<String, ImageIcon> heads) { this.heads=heads; setOpaque(true); setHorizontalAlignment(CENTER); setVerticalAlignment(CENTER); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { String selectValue = (String)value; //设置背景颜色 if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } //从头像的Map中得到当前选择的头像图片 Icon icon = this.heads.get(selectValue); setIcon(icon); if (icon != null) setFont(list.getFont()); return this; } }
相关文章推荐
- JavaScript基础 下拉列表 使用js创建option选项
- HTML基础 form-select创建只有一级的下拉列表,不选的时候只显示一个
- POI 创建联动下拉列表实例
- 简单编程 (二十七)创建窗体下拉列表
- JavaScript基础 下拉列表 使用js创建option选项
- vb.net创建自定义颜色下拉列表
- HTML基础 form-select-optgroup创建两级有标题的下拉列表
- 使用ng-repeat 指令来创建下拉列表
- 后台创建窗体下拉列表
- CRM: 创建CheckBox样式的下拉列表
- 49.java编程思想——创建窗口和程序片 文本 复选框 单选框 下拉列表
- 创建联动下拉列表.
- 使用'VRM_SET_VALUES'函数创建下拉列表
- 创建CheckBox样式的下拉列表
- 小鱼儿的菜鸟笔记四 Excel创建二级下拉列表
- 使用ng-repeat 指令来创建下拉列表2
- 使用标签创建下拉列表和多选列表框
- 模仿combbox的创建下拉列表js支持动态选项
- unity_NGUI系统学习(十一)_PopupList下拉列表(弹出列表)的创建和使用
- 下拉列表DropDownList 创建方法