Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:24:16+00:00 2026-05-24T07:24:16+00:00

I have created customized combobox which extends JCombobox. It is having checkboxes in popup

  • 0

I have created customized combobox which extends JCombobox. It is having checkboxes in popup list. When I try to select/check checkbox inside popup there is not selection color.

I tried to set background to checkbox inside custom renderer but its not working. Here is my code if any ideas please help. Thanks in advance.

import java.awt.*;   
import java.awt.event.*;   
import java.util.ArrayList;

import javax.swing.*;   

public class MyCombo extends JComboBox implements ActionListener  
{ 
    /**
     * Serial version uid
     */
    private static final long serialVersionUID = 1L;

    /**
     * Renderer for CheckBoxListComboBox
     */
    protected CheckComboRenderer renderer;

    /**
     * Editor for CheckBoxListComboBox
     */
    protected ComboEditor editor;

    /**
     * String Buffer that holds currently selected values
     */
    protected StringBuffer selectedValues;

    /**
     * Creates a <code>CheckBoxListComboBox</code> that contains the elements
     * in the specified arrayList.  By default no item becomes selected.
     * @param arrayList ArrayList holds data to display
     */
    public MyCombo()
    {
        renderer = new CheckComboRenderer();
        editor = new ComboEditor();
        selectedValues = new StringBuffer();
        setEditable(true);
        setModel(makeModel());

        addActionListener(this);
        setRenderer(renderer);   
        setEditor(editor);
    }

    /* (non-Javadoc)
     * @see javax.swing.JComboBox#setPopupVisible(boolean)
     */
    public void setPopupVisible(boolean v) 
    {
        //Dont hide popup on click
        //super.setPopupVisible(v);
    }

    /* (non-Javadoc)
     * @see javax.swing.JComboBox#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e)
    {
        JComboBox cb = (JComboBox)e.getSource();   
        CheckComboStore store = (CheckComboStore)cb.getSelectedItem();   
        CheckComboRenderer ccr = (CheckComboRenderer)cb.getRenderer();   
        ccr.checkBox.setSelected((store.state = !store.state));   
        JTextField tf = (JTextField) editor.getEditorComponent();

        StringBuffer buffer = new StringBuffer("");
        DefaultComboBoxModel mdl = (DefaultComboBoxModel) cb.getModel();
        for (int i = 0; i < mdl.getSize(); i++)
        {
            store = (CheckComboStore)mdl.getElementAt(i);

            if (store.getState().booleanValue())
            {
                if (buffer.length() == 0)
                    buffer.append(store.getId());
                else
                    buffer.append("; " + store.getId());
            }
        }
        tf.setText(buffer.toString());
    }

    private DefaultComboBoxModel makeModel()
    {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement(new CheckComboStore("Item1", false));
        model.addElement(new CheckComboStore("Item2", false));
        model.addElement(new CheckComboStore("Item3", false));
        model.addElement(new CheckComboStore("Item4", false));
        model.addElement(new CheckComboStore("Item5", false));
        return model;
    }

    private JPanel getContent()   
    {   
        final JComboBox combo = new JComboBox()
        {
            public void setPopupVisible(boolean v) 
            {
                //Dont hide popup on click
                //super.setPopupVisible(v);
            }
        };
        combo.setPreferredSize(new Dimension(250,25));
        combo.setEditable(true);
        combo.setModel(makeModel());

        combo.addActionListener(this);
        combo.setRenderer(renderer);   
        combo.setEditor(editor);
        JPanel panel = new JPanel(); 
        panel.add(combo);   
        return panel;   
    }   

    public static void main(String[] args)   
    {   
        JFrame f = new JFrame();   
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        f.getContentPane().add(new MyCombo().getContent());   
        f.setSize(300,160);   
        f.setLocation(200,200);   
        f.setVisible(true);   
    }

    class CheckComboRenderer implements ListCellRenderer   
    {   
        JCheckBox checkBox; 
        public CheckComboRenderer()   
        {   
            checkBox = new JCheckBox(); 
        }   

        public Component getListCellRendererComponent(JList list,   
                                                      Object value,   
                                                      int index,   
                                                      boolean isSelected,   
                                                      boolean cellHasFocus)   
        {
            CheckComboStore store = (CheckComboStore)value;   
            checkBox.setText(store.getId());  
            checkBox.setSelected(((Boolean)store.state).booleanValue());   
            checkBox.setBackground(isSelected ? Color.red : Color.white);   
            return checkBox;
        }   
    }

    class ComboEditor implements ComboBoxEditor 
    {   
        JTextField tf;   

        public ComboEditor() 
        {   
            tf = new JTextField(""); 
        }   

        public void addActionListener( ActionListener l ) {}   

        public void addKeyListener( KeyListener k){} 

        public Component getEditorComponent() 
        {   
            return tf;                                   
        }   

        public Object getItem() 
        {   
            return tf.getText();   
        }   

        public void removeActionListener( ActionListener l ) {}   

        public void selectAll() {}   

        public void setItem( Object o ) {}   
    }  

    class CheckComboStore   
    {   
        String id = "";   
        Boolean state = false;
        public String toString()
        {
            return id;
        }

        public CheckComboStore(String id, Boolean state)   
        {   
            this.id = id;   
            this.state = state;   
        } 

        public String getId()
        {
            return id;
        }

        public Boolean getState()
        {
            return state;
        }
    }  
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:24:17+00:00Added an answer on May 24, 2026 at 7:24 am

    See from DefaultCellRenderer source

        bg = DefaultLookup.getColor(this, ui, "List.dropCellBackground");
        fg = DefaultLookup.getColor(this, ui, "List.dropCellForeground");
    
    if (isSelected) {
        setBackground(bg == null ? list.getSelectionBackground() : bg);
        setForeground(fg == null ? list.getSelectionForeground() : fg);
    }
    else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a customized BasicEditField with Border using Bitmap.Now while typing the text,it
On the List.aspx page I have customized the columns with names. And I have
I have created a customized call that is called in UITableViewCell (cellForRowAtIndexPath) . Everything
I have a separate method to create customized UITableViewCell, Which is given Below -(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
I have created a custom button called ModuleUIButton which inherits from UIButton . This
I have created customized menu and toolbar in MS Excel. I wanted to show
I have created a customized System Service, and i want my service to capture
I have a Wordpress website that was customized by a php developer. He created
I created a project for iPad which uses Apple's Use CoreData template. I customized
i have created customized analytic code for my clients. It tracks visits and conversions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.