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 眼鏡蛇 閱讀(2111) 評論(0)  編輯  收藏 所屬分類: AWT/SWING/SWT

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 从化市| 宁陕县| 六枝特区| 长治县| 浏阳市| 克山县| 神农架林区| 乌兰浩特市| 广德县| 阜康市| 海盐县| 章丘市| 江西省| 韩城市| 镇坪县| 凤翔县| 龙州县| 新野县| 汶上县| 青浦区| 天长市| 阜平县| 温宿县| 正阳县| 淮南市| 清河县| 浠水县| 康保县| 舟山市| 晋州市| 张家界市| 图们市| 平武县| 达拉特旗| 景洪市| 祁连县| 辽宁省| 友谊县| 西乌珠穆沁旗| 栾城县| 微博|