<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲欧洲精品无码AV| 在线亚洲v日韩v| 丁香五月亚洲综合深深爱| 处破痛哭A√18成年片免费| 十八禁无码免费网站| 一级视频在线免费观看| 亚洲一区二区三区国产精华液| 女人裸身j部免费视频无遮挡| 免费H网站在线观看的| 免费看无码特级毛片| 九九免费观看全部免费视频| 亚洲人成自拍网站在线观看| 亚洲高清日韩精品第一区| 亚洲一区二区高清| 国产一区视频在线免费观看| 免费黄色小视频网站| 成年网站免费视频A在线双飞| 亚洲AV网一区二区三区 | 在线观看亚洲天天一三视| 女人18毛片水最多免费观看| 99无码人妻一区二区三区免费| 亚洲色成人四虎在线观看| 亚洲国产av一区二区三区丶| 久久精品国产亚洲AV麻豆网站| 日本v片免费一区二区三区| 毛片基地免费视频a| 手机在线毛片免费播放| 91免费播放人人爽人人快乐| 最近中文字幕完整免费视频ww | 亚洲一级免费视频| 久久免费精彩视频| 久久午夜羞羞影院免费观看| 国产无遮挡裸体免费视频在线观看| 亚洲综合色丁香婷婷六月图片 | 国产黄在线观看免费观看不卡| 久久精品国产亚洲77777| 亚洲国产成人久久精品动漫| 久久久久无码精品亚洲日韩| 亚洲国产第一页www| 亚洲视频小说图片| 亚洲最大的视频网站|