I'm aware of JList
and JComboBox
. I need the combo box drop down functionality with multiple selection functionality that JList
provides.
This is because the contents of the list are too huge to be displayed using a simple list. I also need to select multiple items, otherwise I would have been content with JComboBox
.
Any suggestions?
When using multi-select, it's better to use a list than a combo box. As GUI metaphors go, people expect a combo box to be single select, whereas lists can be either.
Place the
JList
in aJScrollPane
. You can call setVisibleRowCount(int) on theJList
to specify how many rows at a time should be shown.You can make a custom cell renderer for the combobox and add checkboxes to that components, so you can check and uncheck them. You have to make something like this:
If your data has a hierarchical character, consider NetBeans'
Outline
component, discussed in Announcing the new Swing Tree Table and in this answer. Here's the Current Development Version of the API.To achieve the described functionality, I finally decided to "abuse" the
JMenuBar
and add to it severalJCheckBoxMenuItems
. The GUI then perfectly fits the purpose (at least for me), it is just the handling of the ItemEvent that risks to become a bit annoying.(finally there, I defined some bit logic over the items, and then may restrict myself to handling only one type of event)