0

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?

Rory Lester
  • 2,858
  • 11
  • 49
  • 66

1 Answers1

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