Top Description Inners Fields Constructors Methods
com.dlsc.gemsfx

public Class SearchField<T>

extends Control
Class Inheritance
Known Direct Subclasses
com.dlsc.gemsfx.TagsField
Type Parameters
<T>
the type of objects to work on
Imports
com.dlsc.gemsfx.skins.SearchFieldPopup, .SearchFieldSkin, com.dlsc.gemsfx.util.HistoryManager, .StringHistoryManager, javafx.animation.Animation, .RotateTransition, javafx.beans.binding.Bindings, javafx.beans.property.BooleanProperty, .DoubleProperty, .ListProperty, .ObjectProperty, .ReadOnlyBooleanProperty, .ReadOnlyBooleanWrapper, .ReadOnlyStringProperty, .ReadOnlyStringWrapper, .SimpleBooleanProperty, .SimpleDoubleProperty, .SimpleListProperty, .SimpleObjectProperty, .SimpleStringProperty, .StringProperty, javafx.collections.FXCollections, .ObservableList, javafx.concurrent.Service, .Task, javafx.css.PseudoClass, javafx.event.Event, .EventHandler, .EventType, javafx.scene.Node, javafx.scene.control.ContentDisplay, .Control, .Label, .ListCell, .ListView, .Skin, .TextField, javafx.scene.input.KeyCode, .KeyCombination, .KeyEvent, javafx.scene.layout.Region, javafx.scene.text.Text, .TextFlow, javafx.util.Callback, .Duration, .StringConverter, org.apache.commons.lang3.StringUtils, org.apache.commons.lang3.builder.ToStringBuilder, org.kordamp.ikonli.javafx.FontIcon, org.kordamp.ikonli.materialdesign.MaterialDesign, java.util.Collection, .Collections, .Comparator, .Objects, java.util.function.BiFunction, .Consumer

The search field is a standard text field with auto suggest capabilities and a selection model for a specific type of object. This type is defined by the generic type argument. The main difference to other auto-suggest text fields is that the main outcome of this field is an object and not just the text entered by the user. Another difference is how the text field automatically finds and selects the first object that matches the text typed by the user so far. A third feature of this control is its ability to create new instances of the specified object type if no matching object can be found in the list of objects returned by the suggestion provider. This last feature allows an application to let the user either pick an existing object or to create a new one on-the-fly (but only if a new item producer has been set).

The search field requires proper configuration to work correctly:

  1. Suggestion Provider - a callback that returns a collection of items for a given search field suggestion request. The suggestion provider is invoked asynchronously via JavaFX concurrency API (service & task). The suggestion provider gets invoked slightly delayed whenever the user types some text into the field. If the user types again the current search gets cancelled and a new search gets initiated. As long as the user types fast enough the actual search will not be performed.
  2. Converter - the converter is used to convert the items found in the suggestions list to text. This is just a standard StringConverter instance (only the toString() method needs to be implemented).
  3. Cell Factory - a standard list cell factory / callback used for the ListView instance shown in the popup that presents the suggested items. The default cell factory should be sufficient for most use cases. It simply displays the name of the items via the help of the string converter. However, it also underlines the text match in the name.
  4. Matcher - a function taking two arguments that will be applied to the suggested items to find "perfect matches" for the given search text (entered by the user). The function takes an item and the search text as input and returns a boolean. The first perfect match found will be used to autocomplete the text of the search field.
  5. New Item Producer - a callback that returns a new item instance of the type supported by the search field. This callback is used if the field is configured to create items "on-the-fly", meaning the typed text does not match anything in the suggested list of items.
  6. Comparator - a standard comparator used to perform a first sorting of the suggested items. However, internally the field wraps this comparator to place some items higher up in the dropdown list as they are better matches for the current search text.

The history manager is disabled by default, but it can be enabled using the setHistoryManager(HistoryManager) method. We have implemented a local history manager, StringHistoryManager, which uses the Java Preferences API to store history records. You can enable it via the setHistoryManager(HistoryManager) method.

See Also
setSuggestionProvider(Callback), setConverter(StringConverter), setCellFactory(Callback), setMatcher(BiFunction), setNewItemProducer(Callback), setComparator(Comparator)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SearchField.SearchEvent

An event type used by the SearchField to indicate the start and end of searching operations.

private class
public static class
SearchField.SearchFieldListCell<
the type of the cell
T
>

A custom list cell implementation that is capable of underlining the part of the text that matches the user-typed search text.

public static interface
SearchField.SearchFieldSuggestionRequest

Represents a suggestion fetch request.

private class
private class

Field Summary

Modifier and TypeField and Description
private BooleanProperty
private BooleanProperty
private BooleanProperty
private BooleanProperty
private final ReadOnlyStringWrapper
private final DoubleProperty
private final ObjectProperty<Node>
private final ObjectProperty<Callback<ListView<T>, ListCell<T>>>
private boolean
private final ObjectProperty<Comparator<T>>
private final ObjectProperty<StringConverter<T>>
private static final boolean
private static final boolean
private static final boolean
private static final String
private static final PseudoClass
private final TextField
private final ReadOnlyStringWrapper
private final ObjectProperty<Node>
private final BooleanProperty
private final BooleanProperty
private final HistoryButton<String>
private ObjectProperty<HistoryManager<String>>
private ObjectProperty<Node>
private final ObjectProperty<Node>
private final ObjectProperty<BiFunction<T, String, Boolean>>
private final ReadOnlyBooleanWrapper
private final ObjectProperty<Callback<String, T>>
private final ObjectProperty<Consumer<T>>
private SearchField<T>.SearchEventHandlerProperty
private SearchField<T>.SearchEventHandlerProperty
private ObjectProperty<Node>
private final SearchFieldPopup<T>
private final StringProperty
private final ObservableList<T>
private final ObjectProperty<Node>
private final ReadOnlyBooleanWrapper
private final SearchField<T>.SearchService
private final ObjectProperty<T>
private final BooleanProperty
private final ObjectProperty<Callback<SearchField.SearchFieldSuggestionRequest, Collection<T>>>
private final ListProperty<T>
private final StringProperty

Constructor Summary

AccessConstructor and Description
public
SearchField()

Constructs a new spotlight field.

Method Summary

Modifier and TypeMethod and Description
public final BooleanProperty

Returns:

true if the text should be added to the history on commit, false otherwise
addingItemToHistoryOnCommitProperty
()

Determines whether the text of the text field should be added to the history when the user commits to a value.

public final BooleanProperty

Returns:

true if the text should be added to the history on Enter, false otherwise
addingItemToHistoryOnEnterProperty
()

Determines whether the text of the text field should be added to the history when the user presses the Enter key.

public final BooleanProperty

Returns:

true if the text should be added to the history on focus lost, false otherwise
addingItemToHistoryOnFocusLostProperty
()

Determines whether the text of the text field should be added to the history when the field losses its focus.

private void
public final BooleanProperty

Returns:

the BooleanProperty for autoCommitOnFocusLost.
autoCommitOnFocusLostProperty
()

Returns the BooleanProperty that indicates if text should auto-commit when the field loses focus.

public final ReadOnlyStringProperty

Returns:

the auto-completed text (e.g. "ates" after the user entered "United St" in a country search field).
autoCompletedTextProperty
()

A read-only property containing the automatically completed text.

public final DoubleProperty

Returns:

the gap (in pixels) between the user typed text and the autocompleted text
autoCompletionGapProperty
()

Defines the gap (in pixels) between the user typed text and the autocompleted text.

public final ObjectProperty<Node>

Returns:

the busy graphic
busyGraphicProperty
()

Stores a node that will be shown on the field's right side whenever a search is ongoing.

public final void
cancel()

Cancels the current search in progress.

public final ObjectProperty<Callback<ListView<T>, ListCell<T>>>

Returns:

the cell factory used by the suggestion list view
cellFactoryProperty
()

A cell factory that can be used by a list view to visualize the list of suggestions.

public void
clear()

Convenience method to invoke clear() on the text field.

public void
commit()

Makes the field commit to the currently selected item and updates the field to show the full text provided by the converter for the item.

public final ObjectProperty<Comparator<T>>

Returns:

the sorting comparator used for the suggestions list
comparatorProperty
()

A comparator used to sort the list of suggestions.

public final ObjectProperty<StringConverter<T>>

Returns:

the converter for turning the objects returned by the suggestion provider into text
converterProperty
()

A converter for turning the objects returned by the suggestion provider into text.

protected Skin<?>
createDefaultSkin()

Overrides javafx.scene.control.Control.createDefaultSkin.

Create a new instance of the default skin for this control.

private HistoryButton<String>
public final ReadOnlyStringProperty

Returns:

the full text shown by the text field
fullTextProperty
()

A read-only property containing the concatenation of the regular text of the text field and the autocompleted text.

public final String
public final double
public final Node
public final Callback<ListView<T>, ListCell<T>>
public final Comparator<T>
public final StringConverter<T>
public final TextField

Returns:

the text field editor control
getEditor
()

Returns the text field control used for editing the text.

public final String
public final Node
public final HistoryManager<String>
public final Node
public final Node
public final BiFunction<T, String, Boolean>
public final Callback<String, T>
public final Consumer<T>
public final EventHandler<SearchField.SearchEvent>
public final EventHandler<SearchField.SearchEvent>
public final Node
public final SearchFieldPopup<T>
public final String
public final Node
public final T
public final Callback<SearchField.SearchFieldSuggestionRequest, Collection<T>>
public final ObservableList<T>

Returns:

the list of suggestions
getSuggestions
()

Returns a read-only (unmodifiable) list of the current suggestions.

public final String
public String
getUserAgentStylesheet()

Overrides javafx.scene.layout.Region.getUserAgentStylesheet.

An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method.

public final ObjectProperty<Node>

Returns:

the field's graphic
graphicProperty
()

Stores a node that will be shown on the field's right-hand side whenever the field is idle.

public final BooleanProperty

Returns:

true if the popup should not be shown when there are no suggestions to display.
hidePopupWithNoChoiceProperty
()

Determines whether to hide the popup window when there are no choices available in the suggestion list.

public final BooleanProperty

Returns:

true if the popup showing the list of suggestions will not appear if only a single choice is available
hidePopupWithSingleChoiceProperty
()

Hides the popup window with the suggestion list if the list only contains a single elements.

public final ObjectProperty<HistoryManager<String>>

Returns:

the property representing the history manager
historyManagerProperty
()

The history manager that is used to manage the history of the SearchField.

public final ObjectProperty<Node>

Returns:

the property representing the history placeholder node
historyPlaceholderProperty
()

Returns the property representing the history placeholder node.

private void
public final boolean
public final boolean
public final boolean
public final boolean

Returns:

true if auto-commit on focus lost is enabled, false otherwise.
isAutoCommitOnFocusLost
()

Checks if the auto-commit on focus lost feature is enabled.

public final boolean
public final boolean
public final boolean
public final boolean
public final boolean
public final ObjectProperty<Node>

Returns:

a custom node for the left-hand side (e.g. "clear" button)
leftProperty
()

A custom node that can be shown on the left-hand side of the field.

public final ObjectProperty<BiFunction<T, String, Boolean>>

Returns:

the function used for determining the best match in the suggestion list
matcherProperty
()

The function that is used to determine the first item in the suggestion list that is a good match for auto selection.

public final ObjectProperty<Callback<String, T>>

Returns:

the callback for producing a new object of the field supported object type
newItemProducerProperty
()

A callback used for creating a new object on-the-fly if no item matches the search text.

public final ReadOnlyBooleanProperty

Returns:

true if the selected item was not part of the suggestion list and has been created on-the-fly
newItemProperty
()

Determines if the selected item has been created on-the-fly via the newItemProducer.

public final ObjectProperty<Consumer<T>>

Returns:

the commit handler
onCommitProperty
()

A callback that gets invoked when the user has committed to the selected value.

private void
public final ObjectProperty<EventHandler<SearchField.SearchEvent>>

Returns:

the "search finished" event handler
onSearchFinishedProperty
()

An event handler that can be used to get informed whenever the field finishes a search.

public final ObjectProperty<EventHandler<SearchField.SearchEvent>>

Returns:

the "search started" event handler
onSearchStartedProperty
()

An event handler that can be used to get informed whenever the field starts a search.

public final ObjectProperty<Node>

Returns:

the placeholder property for the list view of the auto suggest popup
placeholderProperty
()

The placeholder UI when no suggestions have been returned by the suggestion provider.

public final StringProperty

Returns:

the prompt text
promptTextProperty
()

A convenience property to set the prompt text shown by the text field when no text has been entered yet (e.g. "Search ...").

public final ObjectProperty<Node>

Returns:

a custom node for the right-hand side (e.g. "clear" button)
rightProperty
()

A custom node that can be shown on the right-hand side of the field.

public final ReadOnlyBooleanProperty

Returns:

true if the search is currently in progress
searchingProperty
()

A flag indicating whether the asynchronous search is currently in progress.

public void
select(T
the selected item
item
)

Selects the given item and sets the editor's text to the string provided by the converter for the item.

public final ObjectProperty<T>

Returns:

the selected item
selectedItemProperty
()

Contains the currently selected item.

public final void
setAddingItemToHistoryOnCommit(boolean addingItemToHistoryOnCommit)

public final void
setAddingItemToHistoryOnEnter(boolean addingItemToHistoryOnEnter)

public final void
setAddingItemToHistoryOnFocusLost(boolean addingItemToHistoryOnFocusLost)

public final void
setAutoCommitOnFocusLost(boolean
if true, enables auto-commit on focus lost; if false, disables it.
value
)

Sets the value of the autoCommitOnFocusLost property.

public final void
setAutoCompletionGap(double autoCompletionGap)

public final void
setBusyGraphic(Node busyGraphic)

public final void
public final void
setComparator(Comparator<T> comparator)

public final void
public final void
setGraphic(Node graphic)

public final void
setHidePopupWithNoChoice(boolean hidePopupWithNoChoice)

public final void
setHidePopupWithSingleChoice(boolean hidePopupWithSingleChoice)

public final void
public final void
setHistoryPlaceholder(Node historyPlaceholder)

public final void
setLeft(Node left)

public final void
public final void
setNewItemProducer(Callback<String, T> newItemProducer)

public void
setOnCommit(Consumer<T> onCommit)

public final void
public final void
public final void
public final void
setPromptText(String promptText)

public final void
setRight(Node right)

public final void
setSelectedItem(T selectedItem)

public final void
setShowSearchIcon(boolean showSearchIcon)

public final void
public final void
public final BooleanProperty

Returns:

true if a search icon will be shown
showSearchIconProperty
()

Determines if the field will show an icon on the right-hand side which indicates that the field is a search field.

public final ObjectProperty<Callback<SearchField.SearchFieldSuggestionRequest, Collection<T>>>

Returns:

#getSuggestions
suggestionProviderProperty
()

A callback used for looking up a list of suggestions for the current search text.

public final StringProperty

Returns:

the text shown by the field
textProperty
()

A convenience property bound to the editor's text property.

protected void
update(Collection<T>
the new suggestions to use for the field
newSuggestions
)

Updates the control with the newly found list of suggestions.

Inherited from javafx.scene.control.Control:
computeMaxHeightcomputeMaxWidthcomputeMinHeightcomputeMinWidthcomputePrefHeightcomputePrefWidthcontextMenuPropertyexecuteAccessibleActiongetBaselineOffsetgetClassCssMetaDatagetContextMenugetControlCssMetaDatagetCssMetaDatagetInitialFocusTraversablegetSkingetTooltipisResizablelayoutChildrenqueryAccessibleAttributesetContextMenusetSkinsetTooltipskinPropertytooltipProperty