I have a JavaFx ComboBox containing a long list of items. To find the item I want, when the ComboBox drops down I start typing part of the description and filter for items containing what I type in the descriptions showing in the ComboBox, and the list in the ComboBox shrinks accordingly. That works fine. However, I want to be able to include a spacebar (blank) as part of the search string, but as soon as I press the spacebar, the combobox hides.
I have tried adding an event filter, which does catch the spacebar (and all other keys), but not before the ComboBox hides itself when the spacebar is pressed. Any suggestions on preventing the spacebar from causing the ComboBox to hide would be appreciated. This is the truncated code showing what I have tried.
class MainController
{
@FXML
lateinit var cmbPwds: ComboBox<PwdItem>
init {
Platform.runLater {setKeyboardEvent()}
}
fun setKeyboardEvent() {
cmbPwds.addEventFilter(KeyEvent.ANY) {event ->
event.consume()
if(event.eventType == KeyEvent.KEY_TYPED)
print(if(event.character == " ") "BLANK" else event.character)
}
}
}