2 Replies Latest reply on Dec 13, 2010 7:27 AM by darrylburke

    Checkbox items in Combobox rendering

      Hi All,
      I am trying to show checkboxes in combobox i.e. each element in the combobox will be a checkbox so that user can

      select multiple options in the combobox. This will be same as "AutoFilter Option in MS Excel 2007". After much

      googling around, I have the following solution with only one issue pending:
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import java.awt.BorderLayout;
      import javax.swing.JComboBox;
      import java.awt.event.ActionEvent;
      import javax.swing.JList;
      import javax.swing.ListCellRenderer;
      import javax.swing.JCheckBox;
      import java.awt.Component;
      import java.awt.Color;
      public class CheckCombo1
          extends JFrame {
        JPanel buttonPanel;
        CheckCombo checkCombo;
        public CheckCombo1() {
          buttonPanel = new JPanel();
          checkCombo = new CheckCombo();
          checkCombo.addItem(new CheckComboStore("Item-A", true));
          checkCombo.addItem(new CheckComboStore("Item-B", true));
          checkCombo.addItem(new CheckComboStore("Item-C", true));
          getContentPane().add(buttonPanel, BorderLayout.NORTH);
          setSize(650, 250);
        public static void main(String args[]) {
          CheckCombo1 fe = new CheckCombo1();
        class CheckCombo
            extends JComboBox {
          CheckComboRenderer renderer;
          public CheckCombo() {
            renderer = new CheckComboRenderer();
          public void actionPerformed(ActionEvent e) {
            CheckComboStore store = (CheckComboStore) getSelectedItem();
      //      CheckComboRenderer ccr = (CheckComboRenderer) getRenderer();
            store.state = !store.state;
      //      ccr.checkBox.setSelected(store.state);
          public void setPopupVisible(boolean flag) {
        class CheckComboRenderer
            implements ListCellRenderer {
          JCheckBox checkBox;
          public CheckComboRenderer() {
            checkBox = new JCheckBox();
      //      checkBox.addActionListener(new java.awt.event.ActionListener() {
      //        public void actionPerformed(ActionEvent e) {
      //          System.out.println("here");
      //        }
      //      }
      //      );
          public Component getListCellRendererComponent(JList list,
                                                        Object value,
                                                        int index,
                                                        boolean isSelected,
                                                        boolean cellHasFocus) {
            CheckComboStore store = (CheckComboStore) value;
            System.out.println("text=" + store.id + " state=" +
            checkBox.setSelected( ( (Boolean) store.state).booleanValue());
            checkBox.setBackground(isSelected ? Color.red : Color.white);
            checkBox.setForeground(isSelected ? Color.white : Color.black);
            return checkBox;
        class CheckComboStore {
          String id;
          Boolean state;
          public CheckComboStore(String id, Boolean state) {
            this.id = id;
            this.state = state;
      When you run the above code, you will see a combo box with three elements, "Item-A", "Item-B" and "Item-C". All the

      three elements are checkboxes so that user can select multiple options. However, if you will select the option which

      is already selected in the combobox, its checkbox will not be rendered correctly, even though the state of the

      checkbox has changed correctly.
      Ex: On running the sample application, if you open the combobox and select the "Item-A", its checkbox will not be

      rendered correctly. If you move the mouse over other item (with pop up already opened), you will see the state of

      the "Item-A" checkbox has indeed changed but it is not showing correctly. The same thing happens whenever you select

      a already selected item in the combobox. It seems there is some rendering issue.
      Please help.

      Edited by: user13464074 on Dec 12, 2010 9:33 PM