类 SearchableComboBoxSkin<T>

java.lang.Object
javafx.scene.control.SkinBase<javafx.scene.control.ComboBox<T>>
com.tlcsdm.core.javafx.control.skin.SearchableComboBoxSkin<T>
所有已实现的接口:
javafx.scene.control.Skin<javafx.scene.control.ComboBox<T>>

public class SearchableComboBoxSkin<T> extends javafx.scene.control.SkinBase<javafx.scene.control.ComboBox<T>>
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

  • Pressing ENTER: the selected item is applied, the popup closes. If no item is selected, the first item is applied. To select another item the cursor keys can be used before pressing ENTER.
  • Pressing TAB: Same as ENTER, but in addition the focus is transferred to the next control.
  • Selecting an item using the mouse closes the popup.

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.

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 Control.setSkin(Skin) or in CSS.

另请参阅:
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    private final javafx.scene.control.ComboBox<T>
    A "normal" combobox used internally as a delegate to get the default combo box behavior.
    private static final javafx.scene.image.Image
     
    private T
    Used when pressing ESC
    private final CustomTextField
    The search field shown when the popup is shown.
  • 构造器概要

    构造器
    构造器
    说明
    SearchableComboBoxSkin(javafx.scene.control.ComboBox<T> comboBox)
     
  • 方法概要

    修饰符和类型
    方法
    说明
    private void
     
    private void
    checkApplyAndCancel(javafx.scene.input.KeyEvent e)
    Used to alter the behaviour.
    private void
    checkOpenPopup(javafx.scene.input.KeyEvent e)
    Show the popup on UP, DOWN, and on beginning typing a word.
    private javafx.scene.control.ComboBox<T>
     
    private javafx.collections.transformation.FilteredList<T>
     
     
    private String
    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)
     
    private Predicate<T>
    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
    The default behavior of the ComboBoxListViewSkin is to close the popup on ENTER and SPACE, but we need to override this behavior.
    private void
    Called every time the filter text changes.

    从类继承的方法 javafx.scene.control.SkinBase

    computeBaselineOffset, computeMaxHeight, computeMaxWidth, computeMinHeight, computeMinWidth, computePrefHeight, computePrefWidth, consumeMouseEvents, dispose, executeAccessibleAction, getChildren, getClassCssMetaData, getCssMetaData, getNode, getSkinnable, layoutInArea, layoutInArea, layoutInArea, positionInArea, positionInArea, pseudoClassStateChanged, queryAccessibleAttribute, registerChangeListener, registerInvalidationListener, registerListChangeListener, snappedBottomInset, snappedLeftInset, snappedRightInset, snappedTopInset, snapPosition, snapPositionX, snapPositionY, snapSize, snapSizeX, snapSizeY, snapSpace, snapSpaceX, snapSpaceY, unregisterChangeListeners, unregisterInvalidationListeners, unregisterListChangeListeners

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    从接口继承的方法 javafx.scene.control.Skin

    install
  • 字段详细资料

    • filterIcon

      private static final javafx.scene.image.Image filterIcon
    • filteredComboBox

      private final javafx.scene.control.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.
    • searchField

      private final CustomTextField searchField
      The search field shown when the popup is shown.
    • previousValue

      private T previousValue
      Used when pressing ESC
  • 构造器详细资料

    • SearchableComboBoxSkin

      public SearchableComboBoxSkin(javafx.scene.control.ComboBox<T> comboBox)
  • 方法详细资料

    • layoutChildren

      protected void layoutChildren(double x, double y, double w, double h)
      覆盖:
      layoutChildren 在类中 javafx.scene.control.SkinBase<javafx.scene.control.ComboBox<T>>
    • createSearchField

      private CustomTextField createSearchField()
    • createFilteredComboBox

      private javafx.scene.control.ComboBox<T> createFilteredComboBox()
    • bindSearchFieldAndFilteredComboBox

      private void bindSearchFieldAndFilteredComboBox()
    • createFilteredList

      private javafx.collections.transformation.FilteredList<T> createFilteredList()
    • updateFilter

      private void updateFilter()
      Called every time the filter text changes.
    • predicate

      private Predicate<T> predicate()
      Return the Predicate to filter the popup items based on the search field.
    • predicate

      private Predicate<T> predicate(String searchText)
      Return the Predicate to filter the popup items based on the given search text.
    • getDisplayText

      private String getDisplayText(T value)
      Create a text for the given item, that can be used to compare with the filter text.
    • preventDefaultComboBoxKeyListener

      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.
    • checkApplyAndCancel

      private void checkApplyAndCancel(javafx.scene.input.KeyEvent e)
      Used to alter the behaviour. React on Enter, Tab and ESC.
    • checkOpenPopup

      private void checkOpenPopup(javafx.scene.input.KeyEvent e)
      Show the popup on UP, DOWN, and on beginning typing a word.