Results 1 to 3 of 3
  1. #1
    charanjeet_sm is offline Member
    Join Date
    Dec 2010
    Rep Power

    Default Checkbox items in combox 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:
    Java Code:
    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=" + + " state=" +
          checkBox.setSelected( ( (Boolean) store.state).booleanValue());
          checkBox.setBackground(isSelected ? : Color.white);
          checkBox.setForeground(isSelected ? Color.white :;
          return checkBox;
      class CheckComboStore {
        String id;
        Boolean state;
        public CheckComboStore(String id, Boolean state) {
 = 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

  3. #3
    hosscomp is offline Member
    Join Date
    Oct 2010
    Rep Power


    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.
    The combobox was designed to show one selected item. I would think the preferred solution for multiple selections would be a JList in multi-selection mode or a group of check boxes in a JPanel. Either can have a scroll bar and/or be placed in a popup dialog if realestate is a problem.

Similar Threads

  1. Replies: 3
    Last Post: 07-27-2010, 10:52 AM
  2. How to get the selected item from combox
    By man4ish in forum AWT / Swing
    Replies: 5
    Last Post: 12-30-2009, 11:29 AM
  3. CheckBox Jlist items
    By dropez in forum AWT / Swing
    Replies: 1
    Last Post: 03-26-2009, 08:39 PM
  4. Help with JTable rendering
    By daniel2008 in forum AWT / Swing
    Replies: 8
    Last Post: 01-18-2009, 04:51 AM
  5. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 09:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts