I have a jcombobox with multiple entries. I want to filter the list as the user starts typing. How is it possible to hide entries in the jcombobox upon keyboard event?
Asked
Active
Viewed 1,458 times
1 Answers
2
Use regex to filter List of items and just update JCombobox with new ComboBoxModel
.
Something like:
DefaultComboBoxModel comboModel = ((DefaultComboBoxModel)yourComboBox.getModel());
....
// invoke regex on yourArray
comboModel.removeAllElements();
for(int i = 0; i<yourArray.length; i++) {
comboModel.addElement(yourArray[i]);
}
[EDIT]
if you don't want to use two lists: one for actual data, two - for filtered data,
write custom ComboBoxModel where you can implement "hide" ability.

Maxim Shoustin
- 77,483
- 27
- 203
- 225
-
If i use a regular expression, i would have to store the values in another array ... which would imply that i would duplicate data? or am i missing something – Rory Lester Sep 29 '13 at 13:17
-
if you want to "hide", yes use 2 arrays no way. I don't see other way – Maxim Shoustin Sep 29 '13 at 13:18
-
because `removeAllItems` refers to `JCombobox` and here I use `DefaultComboBoxModel` – Maxim Shoustin Sep 29 '13 at 13:28
-
+1 for dynamically updating the model; for reference, a related example is seen [here](http://stackoverflow.com/a/7605780/230513). – trashgod Sep 29 '13 at 13:35