How to create a JCombobox with Checkbox Tree inside of it?

Posted on 2009-04-05
Last Modified: 2012-05-06
Is there any way to create Checkbox Tree inside JCombobox ?
Usually Checkbox Tree is created with custom tree renderer and editor. But since combobox are using ListcellRenderer, is this applicable?

Question by:andriana_ria
LVL 25

Accepted Solution

InteractiveMind earned 50 total points
ID: 24075821
Yes, you use ListCellRenderer for this. See attached code from:

import java.awt.*;  

import java.awt.event.*;  

import javax.swing.*;  


public class CheckCombo implements ActionListener  


    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));  



    private JPanel getContent()  


        String[] ids = { "north", "west", "south", "east" };  

        Boolean[] values =  


            Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE  


        CheckComboStore[] stores = new CheckComboStore[ids.length];  

        for(int j = 0; j < ids.length; j++)  

            stores[j] = new CheckComboStore(ids[j], values[j]);  

        JComboBox combo = new JComboBox(stores);  

        combo.setRenderer(new CheckComboRenderer());  


        JPanel panel = new JPanel();  


        return panel;  



    public static void main(String[] args)  


        JFrame f = new JFrame();  


        f.getContentPane().add(new CheckCombo().getContent());  







/** adapted from comment section of ListCellRenderer api */  

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.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;  



Open in new window


Expert Comment

ID: 34083089
I think the problem is not solved.

The question was about a CheckBoxTree and not a CheckBoxList which is completely different things. The first is more difficult to implement.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 52
backtracking recursion  code 19 53
Java - Why doesn't this JFrame work 3 41
varialbe initialization 11 30
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now