Solved

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

Posted on 2009-04-05
3
2,023 Views
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?

Thanks.
0
Comment
Question by:andriana_ria
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 50 total points
ID: 24075821
Yes, you use ListCellRenderer for this. See attached code from:
http://www.coderanch.com/t/343024/Swing-AWT-SWT-JFace/java/Components-inside-JComboBox

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());  
        combo.addActionListener(this);  
        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 CheckCombo().getContent());  
        f.setSize(300,160);  
        f.setLocation(200,200);  
        f.setVisible(true);  
    }  
}  
   
/** 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.setText(store.id);  
        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;  
    }  
}

Open in new window

0
 

Expert Comment

by:Julien_Bodin
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 411
How to convert from xls to xlsx using java 7 84
sql import cannot be resolved jsp 3 74
junit as external jar or library 7 37
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

740 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