Swing JComboBox实现键值
来源:互联网 发布:黄金比例身高算法 编辑:程序博客网 时间:2024/05/16 19:38
之前用Swing写一个辅助工具项目,发现Swing的JComboBox竟然不能直接支持键值对的形式,很郁闷.
最好自己只能扩展JComboBox.
import java.awt.Component;import java.util.Vector;import javax.swing.DefaultComboBoxModel;import javax.swing.DefaultListCellRenderer;import javax.swing.JComboBox;import javax.swing.JList;import javax.swing.ListCellRenderer;public class KeyValComboBox extends JComboBox{ public KeyValComboBox(Vector values){ super(values); rendererData(); //渲染数据 } public void rendererData(){ ListCellRenderer render = new DefautlListCellRenderer(){ public Component getListCellRendererComponent(JList list,Object value, int index, boolean isSelected, boolean cellHasFocus){ super.getListCellRendererComponent(list, value, indx, isSelected, cellHasFocus); if (value instanceof CheckBoxPo){ CheckBoxPo po = (CheckBoxPo) value; this.setText(po.text); } return this; } }; this.setRenderer(render); } //修改Combox中的数据 pulbic void updateData(Vector values){ setModel(new DefaultComboBoxModel(values)); rendererData(); } @Override public void setSelectedItem(Object anObject){ //选中text与传入的参数相同的项 if (anObject != null){ if (anObject instanceof CheckBoxPo){ super.setSelectedItem(anObject); } if(anObject instanceof String){ for (int index = 0; index < getItemCount(); index++){ CheckBoxPo po = (CheckBoxPo) getItemAt(index); if (po.text.equals(anObject.toString())){ super.setSelectedIndex(index); } } } }else{ super.setSelectedItem(anObject); } } public void setSelectedValue(Object anObject){ //选中value与传入的参数相同的项 if(anObject != null){ if(anObject instanceof CheckBoxPo){ super.setSelectedItem(anObject); } if(anObject instanceof String){ for(int index = 0; index < getItemCount(); index++){ CheckBoxPo po = (CheckBoxPo) getItemAt(index); if(po.value.equals(anObject.toString())){ super.setSelectedIndex(index); } } } }else{ super.setSelectedItem(anObject); } } //获得选中项的键值 public String getSelectedValue(){ if(getSelectedItem() instaceof CheckBoxPo){ CheckBoxPo po = (CheckBoxPo)getSelectItem(); return po.value; } return (getSelectedItem != null) ? getSelectedItem.toString() : null; } //获得选中项的显示文本 public String getSelectedText(){ if(getSelectedItem() instaceof CheckBoxPo){ CheckBoxPo po = (CheckBoxPo)getSelectItem(); return po.text; } return (getSelectedItem != null) ? getSelectedItem.toString() : null; }}用到的实体类
public class CheckBoxPo{ public String value = null; public String text= null; public CheckBoxPo(){} public CheckBoxPo(String value,String text){ this.value = value; this.text = text; }}
使用方法
public KeyValComboBox getCboUpdateRule(){ if(cboUpdateRule == null){ Vector values = new Vector(); CheckBoxPo po = null; for(int i = 0 ; i < 5; i++){ po = new CheckBoxPo(); po.value = i; po.text = "选项"+i; values.add(po); } cboUpdateRule = new KeyValComboBox(values); }}
0 0
- Swing JComboBox实现键值
- JComboBox实现键值绑定
- java swing model实现JComboBox
- Swing-JComboBox
- [Swing]JCombobox组合框效果实现
- Swing组件使用JComboBox
- swing jComboBox 绑定数据库
- 自定义swing.JComboBox下拉事件
- Swing开发之JComboBox篇
- Swing JComboBox的详细使用
- Swing开发之JComboBox篇
- Swing开发之JComboBox篇
- 使用Java Swing的JComboBox实现Html中Select的key-value功能
- Swing 中JComboBox和图片联动
- Swing:支持自动过滤的 JComboBox
- Java Swing: JComboBox removeAllItems()和removeAll()
- JComboBox实现联动效果
- JComboBox
- Jenkins进阶系列之——16一个完整的JENKINS下的ANT BUILD.XML文件
- django+python
- iPhone网络开发之如何使用NSURLConnection
- python操作redis简单例子<转>
- liferay 获取登录用户信息
- Swing JComboBox实现键值
- VS2010 设置main函数输入参数
- apache整合tomcat
- Table Compression
- poj 1753/2965 bfs+位运算(关灯游戏/开冰箱门)
- Jenkins进阶系列之——17Jenkins升级、迁移和备份
- 在做黑马程序员中第10题出错啦,大家帮忙看看
- libmodbus的移植
- 解析 XML - 跨浏览器实现