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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
scoreUp challenge 14 48
json format text only 4 65
Python Assistance 7 36
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

22 Experts available now in Live!

Get 1:1 Help Now