• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using JCheckBox as CellRenderer in JComboBox

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

https://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!
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Biliang Zhou
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic