- java.lang.Object
-
- impl.org.controlsfx.tools.PrefixSelectionCustomizer
-
public class PrefixSelectionCustomizer extends Object
This utility class can be used to customize a
ChoiceBox
orComboBox
and enable the "prefix selection" feature. This will enable the user to type letters or digits on the keyboard while theChoiceBox
orComboBox
has the focus. By default, theChoiceBox
orComboBox
will attempt to select the first item it can find with a matching prefix ignoring case.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 ContorlsFX to allow testing and feedback.
Example
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 - Usage
A common use case is to customize a
ChoiceBox
orComboBox
that has been loaded as part of an FXML. In this case you can use the utility methodscustomize(ChoiceBox)
orcustomize(ComboBox)
. This will install aEventHandler
that monitors theKeyEvent
events.If you are coding, you can also use the preconfigured classes
PrefixSelectionChoiceBox
andPrefixSelectionComboBox
as a substitute forChoiceBox
andComboBox
. In this case,PrefixSelectionComboBox
includes some properties that will allow modifying the elapsed time before the selection is clear, listen to back space to clear the selection, show the popup when the control gains the focus, or set a different lookup criteria.- See Also:
PrefixSelectionChoiceBox
,PrefixSelectionComboBox
-
-
Field Summary
Fields Modifier and Type Field Description static BiFunction<ChoiceBox,String,Optional>
DEFAULT_LOOKUP_CHOICEBOX
static BiFunction<ComboBox,String,Optional>
DEFAULT_LOOKUP_COMBOBOX
static int
DEFAULT_TYPING_DELAY
-
Constructor Summary
Constructors Constructor Description PrefixSelectionCustomizer()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
customize(ChoiceBox<?> choiceBox)
This will install anEventHandler
that monitors theKeyEvent
events to enable the "prefix selection" feature.static void
customize(ComboBox<?> comboBox)
This will install anEventHandler
that monitors theKeyEvent
events to enable the "prefix selection" feature.
-
-
-
Field Detail
-
DEFAULT_TYPING_DELAY
public static final int DEFAULT_TYPING_DELAY
- See Also:
- Constant Field Values
-
DEFAULT_LOOKUP_COMBOBOX
public static final BiFunction<ComboBox,String,Optional> DEFAULT_LOOKUP_COMBOBOX
-
DEFAULT_LOOKUP_CHOICEBOX
public static final BiFunction<ChoiceBox,String,Optional> DEFAULT_LOOKUP_CHOICEBOX
-
-
Method Detail
-
customize
public static void customize(ComboBox<?> comboBox)
This will install anEventHandler
that monitors theKeyEvent
events to enable the "prefix selection" feature. TheEventHandler
will only be installed if theComboBox
is not editable.- Parameters:
comboBox
- TheComboBox
that should be customized- See Also:
PrefixSelectionCustomizer
-
customize
public static void customize(ChoiceBox<?> choiceBox)
This will install anEventHandler
that monitors theKeyEvent
events to enable the "prefix selection" feature.- Parameters:
choiceBox
- TheChoiceBox
that should be customized- See Also:
PrefixSelectionCustomizer
-
-