Multiple Choice JcomboBox

I have jcombobox in my application. and I want it to support multiple selection (as in jlist). is there any code example?

+8
swing jcombobox multi-select
source share
2 answers

I think this is not possible if you are not using JList, as you said.

JComboBox API reports:

The user can select a value from the drop-down list that appears in the user request.

And the JComboBox tutorial :

Lists are not very attractive, but they are more suitable than if the number of elements is large (say, more than 20) or when the selection of several elements may be valid.

Update:

I reviewed this answer because it is actually "possible" to do this using ListCellRenderer by adding a checkbox for each item. Please consider this answer to implement this "solution."

However, I do not think this is a good idea for the following reasons:

  • there is a control such as JList that allows multiple selection;
  • The JComboBox control is intended only for selecting individual elements;
  • for me it makes no sense to use JComboBox and allow multiple selection.
+6
source share

This is not entirely impossible, but there is a lot of work to do the work. You will need to create your own classes to extend / implement all these functions:

  • ListCellRenderer (so that you can mark the selected items when the popup shows and indicates (at least) that there are several options when it is not).
  • JComboBox (obviously)
  • BasicComboBoxUI (actually this is most of the work)
  • BasicListUI (at least I had to)
  • ComboBoxModel
And you will need to develop your own class that implements ItemListener, MouseListener, PopupMenuListener, MouseMotionListener, PropertyChangeListener, KeyListener, ListSelectionListener.
Hint: you need to override the many createXXXListener () methods in the user interface classes to get around many places where multiple selections are selected.
[And he still doesn't allow editable multi-screen combos.]
0
source share

All Articles