views:

247

answers:

2

I understand how to make a multiple-select list box using JLists but I want to add JCheckBoxes to the list and make it dropdown like. The best visual representation I have found online is dropdown-check-list.

What would be the best way to accomplish the above? I was thinking of a TableList. Any suggestions?

+3  A: 

If you are using JList, then its as simple as changing the ListCellRenderer to return a JCheckbox component.

EDIT: For JCombobox, you can use combobox.setRenderer(myListRenderer);

Suraj Chandran
This should work for displaying a Checkbox in a list. How would I make the List look like a ComboBox?
twodayslate
A JComboBox uses a ListCellRenderer to draw its items, just like a JList does: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#setRenderer%28javax.swing.ListCellRenderer%29
Suppressingfire
@twodayslate...see the EDIt in my answer :)
Suraj Chandran
perfect! thanks!
twodayslate
+2  A: 

This code snippet may help you.

The basic idea is to handle actionPerformed or mouseClick events by yourself and keep states of the corresponding items (checked/unchecked) in your own data structure. You'll be able to use that data structure for rendering checkboxes in a dropdown

Dmitry
Exactly what I wanted!
twodayslate