Swing中可多选下拉框的简单实现[转]

  1. 实现可多选下拉框需要写三个类:  
  2.     MyComboBox.java --- 继承自JComboBox  
  3.     CheckListCellRenderer.java --- 继承自JCheckBox,且实现ListCellRenderer  
  4.     CheckValue.java --- 设置JCheckBox的类  
  5.    
  6. 此处也是比较简单的实现,具体为以下为代码:  
  7.   
  8.   
  9. ####MyComboBox.java####  
  10.    
  11. public class MyComboBox extends JComboBox implements ActionListener {  
  12.     public MyComboBox() {  
  13.         addItem(new CheckValue(false"Select All"));  
  14.         this.addActionListener(  
  15.                 new ActionListener() {  
  16.             public void actionPerformed(ActionEvent ae) {  
  17.                 itemSelected();  
  18.             }  
  19.         });  
  20.     }  
  21.   
  22.     private void itemSelected() {  
  23.         if (getSelectedItem() instanceof CheckValue) {  
  24.             if (getSelectedIndex() == 0) {  
  25.                 selectedAllItem();  
  26.             } else {  
  27.                 CheckValue jcb = (CheckValue) getSelectedItem();  
  28.                 jcb.bolValue = (!jcb.bolValue);  
  29.                 setSelectedIndex(getSelectedIndex());  
  30.             }  
  31.             SwingUtilities.invokeLater(  
  32.                     new Runnable() {  
  33.                 public void run() {  
  34.                     /*选中后依然保持当前弹出状态*/  
  35.                     showPopup();  
  36.                 }  
  37.             });  
  38.         }  
  39.     }  
  40.     private void selectedAllItem() {  
  41.         boolean bl = false;  
  42.         for (int i = 0; i < getItemCount(); i++) {  
  43.             CheckValue jcb = (CheckValue) getItemAt(i);  
  44.             if (i == 0) {  
  45.                 bl = !jcb.bolValue;  
  46.             }  
  47.             jcb.bolValue = (bl);  
  48.         }  
  49.         setSelectedIndex(0);  
  50.     }  
  51.     /*获取选取的对象*/  
  52.     public Vector getComboVc() {  
  53.         Vector vc = new Vector();  
  54.         for (int i = 1; i < getItemCount(); i++) {  
  55.             CheckValue jcb = (CheckValue) getItemAt(i);  
  56.             if (jcb.bolValue) {  
  57.                 vc.add(jcb.value);  
  58.             }  
  59.         }  
  60.         return vc;  
  61.     }  
  62. }  
  63.    
  64. ###CheckListCellRenderer.java###  
  65.    
  66. public class CheckListCellRenderer extends JCheckBox implements ListCellRenderer,  
  67.         Serializable {  
  68.     protected static Border noFocusBorder;  
  69.     /** 
  70.      * Constructs a default renderer object for an item 
  71.      * in a list. 
  72.      */  
  73.     public CheckListCellRenderer() {  
  74.         super();  
  75.         if (noFocusBorder == null) {  
  76.             noFocusBorder = new EmptyBorder(1111);  
  77.         }  
  78.         setOpaque(true);  
  79.         setBorder(noFocusBorder);  
  80.     }  
  81.     public Component getListCellRendererComponent(  
  82.             JList list,  
  83.             Object value,  
  84.             int index,  
  85.             boolean isSelected,  
  86.             boolean cellHasFocus) {  
  87.         setComponentOrientation(list.getComponentOrientation());  
  88.         if (isSelected) {  
  89.             setBackground(list.getSelectionBackground());  
  90.             setForeground(list.getSelectionForeground());  
  91.         } else {  
  92.             setBackground(list.getBackground());  
  93.             setForeground(list.getForeground());  
  94.         }  
  95.         if (value instanceof CheckValue) {  
  96.             CheckValue ckValue = (CheckValue) value;  
  97.             this.setText(ckValue.value == null ? "" : ckValue.value);  
  98.             this.setSelected(ckValue.bolValue);  
  99.         }  
  100.         setEnabled(list.isEnabled());  
  101.         setFont(list.getFont());  
  102.         setBorder((cellHasFocus) ?  
  103.                   UIManager.getBorder("List.focusCellHighlightBorder") :  
  104.                   noFocusBorder);  
  105.         return this;  
  106.     }  
  107. }  
  108.    
  109. ###CheckValue.java###  
  110.    
  111. public class CheckValue {  
  112.     public boolean bolValue = false;  
  113.     public String value = null;  
  114.     public CheckValue() {  
  115.     }  
  116.     public CheckValue(boolean bolValue, String value) {  
  117.         this.bolValue = bolValue;  
  118.         this.value = value;  
  119.     }  
  120. }  
  121.    
  122.    
  123.     这三个类放在一个包里,或者简单起见放在一个java文件也可,注意以下inner class和friend class的区别即可。  
  124.     使用方法也很简单:  
  125.    
  126.         for (int i = 0; i < 10; i++) {  
  127.             CheckValue cValue = new CheckValue();  
  128.             cValue.value = "测试_" + i;  
  129.             if (i % 3 == 0) {  
  130.                 cValue.bolValue = true;  
  131.             }  
  132.             jComboBox1.addItem(cValue);  
  133.         }  
  134.         jComboBox1.setRenderer(new CheckListCellRenderer());  
  135.         jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 12));  
  136.   
  137.    
  138.   
  139.     以上是不完整的测试代码,MyComboBox实现的功能是可以多选List项,且占用空间比较小,比传统的JList控件使用也方便,JList一般都是使用Ctrl或Shift键来多选,使用起来不是那么一目了然,MyComboBox还可以实现全选和全部不选的功能,当然这只是非常简单的实现,可以扩展的地方还很多,可以实现多种颜色Item等。





眼镜蛇

posted on 2014-07-30 16:43 眼镜蛇 阅读(1953) 评论(0)  编辑  收藏 所属分类: AWT/SWING/SWT


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜