ComboBox which selects an entry of
its item list based on keyboard input. The user can type letters or
digits on the keyboard and the control will attempt to
select the first item it can find with a matching prefix.
This will only be enabled, when the ComboBox is not editable, so
this class will be setup as non editable by default.
This feature is available natively on the Windows combo box control, so many users have asked for it. There is a feature request to include this feature into JavaFX (Issue RT-18064). The class is published as part of ControlsFX to allow testing and feedback.
Let's look at an example to clarify this. The combo box offers the items ["Aaaaa", "Abbbb", "Abccc", "Abcdd", "Abcde"]. The user now types "abc" in quick succession (and then stops typing). The combo box will select a new entry on every key pressed. The first entry it will select is "Aaaaa" since it is the first entry that starts with an "a" (case ignored). It will then select "Abbbb", since this is the first entry that started with "ab" and will finally settle for "Abccc".
| Keys typed | Element selected | |
|---|---|---|
| a | Aaaaa | |
| aaa | Aaaaa | |
| ab | Abbbb | |
| abc | Abccc | |
| xyz | - |
If you want to modify an existing ComboBox you can use the
PrefixSelectionCustomizer directly to do this.
PrefixSelectionCustomizer
| Modifier and Type | Field and Description |
|---|---|
| private final BooleanProperty | backSpaceAllowed
When allowed, the user can press on the back space to clear the current selection. |
| private final BooleanProperty | displayOnFocusedEnabled
When enabled, the |
| private final ChangeListener | |
| private final ObjectProperty | lookup
Allows setting a custom search criteria, based on the control and the typed selection. |
| private final IntegerProperty | typingDelay
Allows setting the delay until the current selection is reset, in ms. |
| Access | Constructor and Description |
|---|---|
| public | PrefixSelectionComboBox()
Create a non editable |
| Modifier and Type | Method and Description |
|---|---|
| public final BooleanProperty | |
| public final BooleanProperty | |
| public final BiFunction | |
| public final int | |
| public final boolean | |
| public final boolean | |
| public final ObjectProperty | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final IntegerProperty |
| backSpaceAllowed | back to summary |
|---|---|
| private final BooleanProperty backSpaceAllowed When allowed, the user can press on the back space to clear the current selection. Default is false | |
| displayOnFocusedEnabled | back to summary |
|---|---|
| private final BooleanProperty displayOnFocusedEnabled When enabled, the | |
| focusedListener | back to summary |
|---|---|
| private final ChangeListener<Boolean> focusedListener | |
| lookup | back to summary |
|---|---|
| private final ObjectProperty<BiFunction<ComboBox<T>, String, Optional<T>>> lookup Allows setting a custom search criteria, based on the control and the typed selection. The default criteria searchs for the first matching item that starts with the typed selection, being case insenstitive. | |
| typingDelay | back to summary |
|---|---|
| private final IntegerProperty typingDelay Allows setting the delay until the current selection is reset, in ms. Default is 500 ms | |
| PrefixSelectionComboBox | back to summary |
|---|---|
| public PrefixSelectionComboBox() Create a non editable | |
| backSpaceAllowedProperty | back to summary |
|---|---|
| public final BooleanProperty backSpaceAllowedProperty() | |
| displayOnFocusedEnabledProperty | back to summary |
|---|---|
| public final BooleanProperty displayOnFocusedEnabledProperty() | |
| getLookup | back to summary |
|---|---|
| public final BiFunction | |
| getTypingDelay | back to summary |
|---|---|
| public final int getTypingDelay() | |
| isBackSpaceAllowed | back to summary |
|---|---|
| public final boolean isBackSpaceAllowed() | |
| isDisplayOnFocusedEnabled | back to summary |
|---|---|
| public final boolean isDisplayOnFocusedEnabled() | |
| lookupProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| setBackSpaceAllowed | back to summary |
|---|---|
| public final void setBackSpaceAllowed(boolean value) | |
| setDisplayOnFocusedEnabled | back to summary |
|---|---|
| public final void setDisplayOnFocusedEnabled(boolean value) | |
| setLookup | back to summary |
|---|---|
| public final void setLookup(BiFunction<ComboBox<T>, String, Optional<T>> value) | |
| setTypingDelay | back to summary |
|---|---|
| public final void setTypingDelay(int value) | |
| typingDelayProperty | back to summary |
|---|---|
| public final IntegerProperty typingDelayProperty() | |