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,638
  • 11
  • 47
  • 65

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
  • 78,004
  • 28
  • 199
  • 222