The Project        Download   Javadoc   SourceForge

CheckComboBox

ComboBox with CheckBoxes.

Example 1

 List<String> colors = Colors.getColors(); 
  
 CheckComboBox ccb = new CheckComboBox(); 
 ccb.setTextFor(CheckComboBox.NONE, "* any item selected *"); 
 ccb.setTextFor(CheckComboBox.MULTIPLE, "* multiple items *"); 
 ccb.setTextFor(CheckComboBox.ALL, "* all selected *"); 
  
 ListCheckModel model = ccb.getModel(); 
 for (String color : colors) { 
   model.addElement(color); 
 } 


Example 2: disabling items

 List<String> countries = Country.getCountries(); 
  
 CheckComboBox ccb = new CheckComboBox(); 
  
 ListCheckModel model = ccb.getModel(); 
 for (String country : countries) { 
   model.addElement(country); 
 } 
  
 model.addLock("Australia"); 


Example 3: changing renderer

    List<String> countries = Country.getCountries();
 
    CheckComboBox ccb = new CheckComboBox();
 
    ListCheckModel model = ccb.getModel();
    for (String country : countries) {
      model.addElement(country);
    }
 
    ccb.setRenderer(new CheckListRenderer() {
      @Override
      public String getText(Object value) {
        String str = (String) value;
        if (str.equals("Australia")) {
          return str + "*";
        }
        return str;
      }
 
      @Override
      public Component getListCellRendererComponent(JList list, Object value,
                                                    int index,
                                                    boolean isSelected,
                                                    boolean cellHasFocus) {
        Component c =
            super.getListCellRendererComponent(list, value, index, isSelected,
                cellHasFocus);
 
        String str = (String) value;
        if (str.equals("Australia")) {
          c.setForeground(Color.RED);
        } else {
          c.setForeground(Color.BLACK);
        }
 
        // changing default mouse over colors
        if (isSelected) {
          setBackground(Color.BLACK);
          setForeground(Color.WHITE);
        }
        return c;
      }
    });
 


Example 4: adding listeners

 CheckComboBox ccb = new CheckComboBox(); 
  
 ListCheckModel model = ccb.getModel(); 
  
 model.addListLockListener(new ListLockListener() { 
   @Override 
   public void removeLock(ListEvent event) { 
    System.out.println("Lock removed"); 
   } 
  
   @Override 
   public void addLock(ListEvent event) { 
    System.out.println("Lock added"); 
   } 
 }); 
  
 model.addListCheckListener(new ListCheckListener() { 
   @Override 
   public void removeCheck(ListEvent event) { 
    System.out.println("Check removed"); 
   } 
  
   @Override 
   public void addCheck(ListEvent event) { 
    System.out.println("Check added"); 
   } 
 }); 


Example 5: working with locked and checked items

 List<String> countries = Country.getCountries(); 
  
 CheckComboBox ccb = new CheckComboBox(); 
  
 ListCheckModel model = ccb.getModel(); 
 for (String country : countries) { 
   model.addElement(country); 
 } 
  
 model.addCheck("Canada", "Cape Verde"); 
 model.addLock("Canada", "Chile"); 
  
 System.out.println("Checkeds: " + model.getChecksCount()); 
 for (Object checked : model.getCheckeds()) { 
   System.out.println(checked); 
 } 
  
 System.out.println("Lockeds: " + model.getLocksCount()); 
 for (Object locked : model.getLockeds()) { 
   System.out.println(locked); 
 } 

Example 6: embedded component for batch selection

   List<String> countries = Country.getCountries();
 
   CheckComboBox ccb = new CheckComboBox();
 
   ListCheckModel model = ccb.getModel();
   for (String color : countries) {
     model.addElement(color);
   }
 
   BatchSelection bs = new BatchSelection.Link();
   EmbeddedComponent comp = new EmbeddedComponent(bs);
   ccb.setEmbeddedComponent(comp);

Example 7: embedded component for batch selection

   List<String> countries = Country.getCountries();
 
   CheckComboBox ccb = new CheckComboBox();
 
   ListCheckModel model = ccb.getModel();
   for (String color : countries) {
     model.addElement(color);
   }
 
   BatchSelection bs = new BatchSelection.CheckBox();
   EmbeddedComponent comp = new EmbeddedComponent(bs, Anchor.NORTH);
   ccb.setEmbeddedComponent(comp);

Example 8: extending the embedded component for batch selection

   List<String> countries = Country.getCountries();
 
   CheckComboBox ccb = new CheckComboBox();
 
   ListCheckModel model = ccb.getModel();
   for (String color : countries) {
     model.addElement(color);
   }
 
   BatchSelection bs = new CustomBatchSelection();
   EmbeddedComponent comp = new EmbeddedComponent(bs, Anchor.NORTH);
   ccb.setEmbeddedComponent(comp);
 
 
 public static class CustomBatchSelection extends BatchSelection.Link{
 
   private JLabel countLabel;
   private ListCheckListener modelListener;
 
   private JLabel getCountLabel() {
     if (countLabel == null) {
       countLabel = new JLabel("[0]");
     }
     return countLabel;
   }
 
   @Override
   protected void initialization() {
     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
     setLayout(new GridBagLayout());
     GridBagConstraints gbc = new GridBagConstraints();
     gbc.gridx = 0;
     gbc.gridy = 0;
     add(getCountLabel(), gbc);
 
     gbc.gridx = 1;
     gbc.weightx = 1;
     gbc.anchor = GridBagConstraints.EAST;
     gbc.insets = new Insets(0, 10, 0, 0);
     add(getSelectAllButton(), gbc);
 
     gbc.gridx = 2;
     gbc.weightx = 0;
     add(getDeselectAllButton(), gbc);
   }
 
   private void updateCount(int count) {
     getCountLabel().setText("[" + count + "]");
   }
 
   @Override
   protected void unregisterModel() {
     super.unregisterModel();
     if (modelListener != null) {
       getModel().removeListCheckListener(modelListener);
     }
     modelListener = null;
   }
 
   @Override
   protected void registerModel(final ListCheckModel model) {
     super.registerModel(model);
     modelListener = new ListCheckListener() {
       @Override
       public void removeCheck(ListEvent event) {
         updateCount(model.getChecksCount());
       }
 
       @Override
       public void addCheck(ListEvent event) {
         updateCount(model.getChecksCount());
       }
     };
     model.addListCheckListener(modelListener);
   }
 }

Example 9: internationalization

You can change the default button labels replacing the handler internationalization

 I18nManager.setHandlerString(new DefaultHandlerString() { 
   @Override 
   public String getString(String key) { 
     if (key.equals(I18nStringKeys.SELECT_ALL.getKey())) { 
       return "[ SELECT ALL ]"; 
     } else if (key.equals(I18nStringKeys.DESELECT_ALL.getKey())) { 
       return "[ DESELECT ALL ]"; 
     } else if (key.equals(I18nStringKeys.SELECT_DESELECT_ALL.getKey())) { 
       return "[ SELECT / DESELECT ALL ]"; 
     } 
     return super.getString(key); 
   } 
 }); 

 

Partners