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);
       }
}

 

你可能感兴趣的:(Swing,JComboBox,键值)