Swing中可多選下拉框的簡單實現[轉]
- 實現可多選下拉框需要寫三個類:
- 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等。
眼鏡蛇
posted on 2014-07-30 16:43 眼鏡蛇 閱讀(2111) 評論(0) 編輯 收藏 所屬分類: AWT/SWING/SWT