aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using JCheckBox as CellRenderer in JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using JCheckBox as CellRenderer in JComboBox" Watch "Using JCheckBox as CellRenderer in JComboBox" New topic
Author

Using JCheckBox as CellRenderer in JComboBox

Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
Hi ranchers,

A cellRenderer question:

I have three checkBoxes: labelled Monday, Tuesday, and Wednesday, to be used to render one JComboBox. When the JComboBox is clicked, these three checkBoxes should appear in the pull down menu, and they should be allowed to be multi-selected.

I had a question on how to use JCheckBox as CellRenderer in JList before,

http://www.coderanch.com/t/344173/GUI/java/JCheckBox-as-CellRenderer-JList

mainly on how to make the CheckBoxes selectable in the List. This problem is solved by using a MouseListener to locate the item being checked.

Now I face a similar but different situation: use JCheckBox as CellRenderer in JComboBox. A JComboBox does not have method like

JList.locationToIndex(e.getPoint());

Thus I cannot locate which items is being checked. This confuses me...



Thanks in advance!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
I'm having trouble understanding exactly what you are trying to do. You want each item to have a checked-or-not-checked flag, but you also want combo-boxy behavior of being able to make one of the items (or a user-typed item) become the selected item?

But anyway, the popup part of the combo box is a JPopupMenu. Actually it's up to the LnF, and most or all of them use JPopupMenu, but that's what makes it a bit difficult to get your hands on it.


bitguru blog
Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
Sorry I wasn't making myself very clear. What I want is an uneditable JComboBox with a pulldown menu populated by JCheckBoxes. These JCheckBoxes can be multi-selected.

I have figured out how to do this by searching online, and here is an adapted and quoted example:

Note: the key part is one of the methods in the class CheckComboBox: setPopupVisible(boolean flag). It must be overridden to empty, because the default setPopupVisible(boolean flag) will close the pulldown list each time you make a selection with the JCheckBoxes.

****************************************************
CAUTION: freakingly-low quality code
****************************************************



****************************************************
END: freakingly-low quality code
****************************************************
[ February 26, 2007: Message edited by: Biliang Zhou ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
I guess I still don't understand why you are using JComboBox instead of creating your own JPopupMenu from scratch.

What do you expect the JComboBox to look like when the popup is not showing?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using JCheckBox as CellRenderer in JComboBox