Top Description Fields Constructors Methods
impl.org.controlsfx.skin

public Class SearchableComboBoxSkin<T>

extends SkinBase<ComboBox<T>>
Class Inheritance
Imports
javafx.beans.binding.Bindings, javafx.collections.transformation.FilteredList, javafx.scene.control.ComboBox, .ListView, .Skin, .SkinBase, javafx.scene.control.skin.ComboBoxListViewSkin, javafx.scene.image.Image, .ImageView, javafx.scene.input.KeyCode, .KeyEvent, javafx.util.StringConverter, org.controlsfx.control.SearchableComboBox, org.controlsfx.control.textfield.CustomTextField, .TextFields, java.util.Arrays, java.util.function.Predicate

A simple skin for a ComboBox, which shows a search field while the popup is showing. The user can type any text into this search field to filter the popup list.

The user can type multiple words. The popup list is filtered checking if the string representation of an item contains all filter words (ignoring case).

After filtering the list, the user can select an entry by

When pressing ESCAPE while the popup is showing, the item that was selected when the popup opened will be re-selected (even if the user did select another item using the cursor keys.

Other than the ComboBox, the SearchableComboBox does open the Popup when using the cursor keys (the ComboBox does only change the selected item without opening the popup). This combined with the behavior of the ESCAPE key does allow to go through the list of items with the cursor keys and than press ESCAPE to revert the changes.

Screenshot

To better describe what a SearchableComboBox is, please refer to the pictures below:
Screenshot of SearchableComboBox Screenshot of SearchableComboBox Screenshot of SearchableComboBox

Example

Let's look at an example to clarify this. The combo box offers the items ["Berlin", "Bern", "Munich", "Paris", "New York", "Alberta"]. The user now types "ber" into the search field. The combo box popup will only show ["Berlin", "Bern", "Alberta"].

To select the first item ("Berlin"), the user can now either just press ENTER or TAB, or first select this item using the cursor DOWN key and press ENTER or TAB afterwards, or select this item using the mouse.

To select the second or third item, the user either must use the cursor keys first, use the mouse, or type more text until the searched item is the first (or only) item in the list.

If you want to modify an existing ComboBox you can set the skin to SearchableComboBoxSkin (e.g. using ComboBox#setSkin(Skin) or in CSS.

See Also
SearchableComboBox

Field Summary

Modifier and TypeField and Description
private final ComboBox<T>
filteredComboBox

A "normal" combobox used internally as a delegate to get the default combo box behavior.

private static final Image
private T
previousValue

Used when pressing ESC

private final CustomTextField
searchField

The search field shown when the popup is shown.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
private void
checkApplyAndCancel(KeyEvent e)

Used to alter the behaviour.

private void
checkOpenPopup(KeyEvent e)

Show the popup on UP, DOWN, and on beginning typing a word.

private ComboBox<T>
private FilteredList<T>
private CustomTextField
private String
getDisplayText(T value)

Create a text for the given item, that can be used to compare with the filter text.

protected void
layoutChildren(double x, double y, double w, double h)

Overrides javafx.scene.control.SkinBase.layoutChildren.

Called during the layout pass of the scenegraph.

private Predicate<T>
predicate()

Return the Predicate to filter the popup items based on the search field.

private Predicate<T>
predicate(String searchText)

Return the Predicate to filter the popup items based on the given search text.

private void
preventDefaultComboBoxKeyListener()

The default behavior of the ComboBoxListViewSkin is to close the popup on ENTER and SPACE, but we need to override this behavior.

private void
updateFilter()

Called every time the filter text changes.

Inherited from javafx.scene.control.SkinBase:
computeBaselineOffsetcomputeMaxHeightcomputeMaxWidthcomputeMinHeightcomputeMinWidthcomputePrefHeightcomputePrefWidthconsumeMouseEventsdisposeexecuteAccessibleActiongetChildrengetClassCssMetaDatagetCssMetaDatagetNodegetSkinnablelayoutInArealayoutInArealayoutInAreapositionInAreapositionInAreapseudoClassStateChangedqueryAccessibleAttributeregisterChangeListenerregisterInvalidationListenerregisterListChangeListenersnappedBottomInsetsnappedLeftInsetsnappedRightInsetsnappedTopInsetsnapPositionsnapPositionXsnapPositionYsnapSizesnapSizeXsnapSizeYsnapSpacesnapSpaceXsnapSpaceYunregisterChangeListenersunregisterInvalidationListenersunregisterListChangeListeners

Field Detail

filteredComboBoxback to summary
private final ComboBox<T> filteredComboBox

A "normal" combobox used internally as a delegate to get the default combo box behavior. This combo box contains the filtered items and handles the popup.

filterIconback to summary
private static final Image filterIcon
previousValueback to summary
private T previousValue

Used when pressing ESC

searchFieldback to summary
private final CustomTextField searchField

The search field shown when the popup is shown.

Constructor Detail

SearchableComboBoxSkinback to summary
public SearchableComboBoxSkin(ComboBox<T> comboBox)

Method Detail

bindSearchFieldAndFilteredComboBoxback to summary
private void bindSearchFieldAndFilteredComboBox()
checkApplyAndCancelback to summary
private void checkApplyAndCancel(KeyEvent e)

Used to alter the behaviour. React on Enter, Tab and ESC.

checkOpenPopupback to summary
private void checkOpenPopup(KeyEvent e)

Show the popup on UP, DOWN, and on beginning typing a word.

createFilteredComboBoxback to summary
private ComboBox<T> createFilteredComboBox()
createFilteredListback to summary
private FilteredList<T> createFilteredList()
createSearchFieldback to summary
private CustomTextField createSearchField()
getDisplayTextback to summary
private String getDisplayText(T value)

Create a text for the given item, that can be used to compare with the filter text.

layoutChildrenback to summary
protected void layoutChildren(double x, double y, double w, double h)

Overrides javafx.scene.control.SkinBase.layoutChildren.

Doc from javafx.scene.control.SkinBase.layoutChildren.

Called during the layout pass of the scenegraph.

Annotations
@Override
predicateback to summary
private Predicate<T> predicate()

Return the Predicate to filter the popup items based on the search field.

predicateback to summary
private Predicate<T> predicate(String searchText)

Return the Predicate to filter the popup items based on the given search text.

preventDefaultComboBoxKeyListenerback to summary
private void preventDefaultComboBoxKeyListener()

The default behavior of the ComboBoxListViewSkin is to close the popup on ENTER and SPACE, but we need to override this behavior.

updateFilterback to summary
private void updateFilter()

Called every time the filter text changes.