Swing JComboBox实现键值

  之前用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);
       }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值