`
lzth
  • 浏览: 138429 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多
实现可多选下拉框需要写三个类:
    MyComboBox.java --- 继承自JComboBox
    CheckListCellRenderer.java --- 继承自JCheckBox,且实现ListCellRenderer
    CheckValue.java --- 设置JCheckBox的类
 
此处也是比较简单的实现,具体为以下为代码:


####MyComboBox.java####
 
public class MyComboBox extends JComboBox implements ActionListener {
    public MyComboBox() {
        addItem(new CheckValue(false, "Select All"));
        this.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                itemSelected();
            }
        });
    }

    private void itemSelected() {
        if (getSelectedItem() instanceof CheckValue) {
            if (getSelectedIndex() == 0) {
                selectedAllItem();
            } else {
                CheckValue jcb = (CheckValue) getSelectedItem();
                jcb.bolValue = (!jcb.bolValue);
                setSelectedIndex(getSelectedIndex());
            }
            SwingUtilities.invokeLater(
                    new Runnable() {
                public void run() {
                    /*选中后依然保持当前弹出状态*/
                    showPopup();
                }
            });
        }
    }
    private void selectedAllItem() {
        boolean bl = false;
        for (int i = 0; i < getItemCount(); i++) {
            CheckValue jcb = (CheckValue) getItemAt(i);
            if (i == 0) {
                bl = !jcb.bolValue;
            }
            jcb.bolValue = (bl);
        }
        setSelectedIndex(0);
    }
    /*获取选取的对象*/
    public Vector getComboVc() {
        Vector vc = new Vector();
        for (int i = 1; i < getItemCount(); i++) {
            CheckValue jcb = (CheckValue) getItemAt(i);
            if (jcb.bolValue) {
                vc.add(jcb.value);
            }
        }
        return vc;
    }
}
 
###CheckListCellRenderer.java###
 
public class CheckListCellRenderer extends JCheckBox implements ListCellRenderer,
        Serializable {
    protected static Border noFocusBorder;
    /**
     * Constructs a default renderer object for an item
     * in a list.
     */
    public CheckListCellRenderer() {
        super();
        if (noFocusBorder == null) {
            noFocusBorder = new EmptyBorder(1, 1, 1, 1);
        }
        setOpaque(true);
        setBorder(noFocusBorder);
    }
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        setComponentOrientation(list.getComponentOrientation());
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        if (value instanceof CheckValue) {
            CheckValue ckValue = (CheckValue) value;
            this.setText(ckValue.value == null ? "" : ckValue.value);
            this.setSelected(ckValue.bolValue);
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setBorder((cellHasFocus) ?
                  UIManager.getBorder("List.focusCellHighlightBorder") :
                  noFocusBorder);
        return this;
    }
}
 
###CheckValue.java###
 
public class CheckValue {
    public boolean bolValue = false;
    public String value = null;
    public CheckValue() {
    }
    public CheckValue(boolean bolValue, String value) {
        this.bolValue = bolValue;
        this.value = value;
    }
}
 
 
    这三个类放在一个包里,或者简单起见放在一个java文件也可,注意以下inner class和friend class的区别即可。
    使用方法也很简单:
 
        for (int i = 0; i < 10; i++) {
            CheckValue cValue = new CheckValue();
            cValue.value = "测试_" + i;
            if (i % 3 == 0) {
                cValue.bolValue = true;
            }
            jComboBox1.addItem(cValue);
        }
        jComboBox1.setRenderer(new CheckListCellRenderer());
        jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 12));

 

    以上是不完整的测试代码,MyComboBox实现的功能是可以多选List项,且占用空间比较小,比传统的JList控件使用也方便,JList一般都是使用Ctrl或Shift键来多选,使用起来不是那么一目了然,MyComboBox还可以实现全选和全部不选的功能,当然这只是非常简单的实现,可以扩展的地方还很多,可以实现多种颜色Item等。 
  • 大小: 9.4 KB
分享到:
评论
3 楼 眼镜蛇 2014-07-31  
哦,找到了,getComboVc()。开始没细看。不好意思
2 楼 眼镜蛇 2014-07-30  
朋友不知你还能不能看到,这个组件写得非常好。但我想知道,我应该怎么才能得到选中的那几个条目呢?
1 楼 yuyeyouyu 2011-12-05  
请问,这个控件的选择事件如何实现?

相关推荐

Global site tag (gtag.js) - Google Analytics