Class DropDownTextField<T>

Type Parameters:
T - The type of object that this model manipulates
All Implemented Interfaces:
GComponent, ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, SwingConstants
Direct Known Subclasses:
DropDownSelectionTextField

public class DropDownTextField<T> extends JTextField implements GComponent
A text field that handles comparing text typed by the user to the list of objects and then presenting potential matches in a drop down window. The items in this window cannot be selected.

This class will fire fireEditingStopped() and fireEditingCancelled() events when the user makes a choice by pressing the ENTER key, thus allowing the client code to use this class similar in fashion to a property editor. This behavior can be configured to:

This class is subclassed to not only have the matching behavior, but to also allow for user selections.

See Also:
  • Field Details

    • PREVIEW_WINDOW_BGCOLOR

      protected static final Color PREVIEW_WINDOW_BGCOLOR
    • PREVIEW_WINDOW_FGCOLOR

      protected static final Color PREVIEW_WINDOW_FGCOLOR
    • list

      protected DropDownTextField<T>.DropDownList list
    • dataModel

      protected final DropDownTextFieldDataModel<T> dataModel
    • internallyDrivenUpdate

      protected boolean internallyDrivenUpdate
  • Constructor Details

    • DropDownTextField

      public DropDownTextField(DropDownTextFieldDataModel<T> dataModel)
      Constructor.

      Uses the default refresh delay of 350ms.

      Parameters:
      dataModel - provides element storage and search capabilities to this component.
    • DropDownTextField

      public DropDownTextField(DropDownTextFieldDataModel<T> dataModel, int updateMinDelay)
      Constructor.
      Parameters:
      dataModel - provides element storage and search capabilities to this component.
      updateMinDelay - suggestion list refresh delay, triggered after search results have changed. Too low a value may cause an inconsistent view as filtering tasks complete; too high a value delivers an unresponsive user experience.
  • Method Details

    • createListSelectionModel

      protected ListSelectionModel createListSelectionModel()
    • setPreviewPaneAttributes

      protected void setPreviewPaneAttributes()
    • setSelectedItems

      protected void setSelectedItems()
    • getPreviewListener

      protected ListSelectionListener getPreviewListener()
    • getPreviewPaneComponent

      protected JComponent getPreviewPaneComponent()
    • processKeyBinding

      protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
      Overrides:
      processKeyBinding in class JComponent
    • setText

      public void setText(String text)
      Overridden to allow for the setting of text without showing the completion window. This is useful for setting the current value to be edited before the using initiates editing.
      Overrides:
      setText in class JTextComponent
      Parameters:
      text - The text to set on this text field.
    • setTextWithoutClosingCompletionWindow

      protected void setTextWithoutClosingCompletionWindow(String text)
    • hasPreview

      protected boolean hasPreview()
    • getMatchingData

      protected List<T> getMatchingData(String searchText)
    • isMatchingListShowing

      public boolean isMatchingListShowing()
    • setConsumeEnterKeyPress

      public void setConsumeEnterKeyPress(boolean consume)
      When true, this field will not pass Enter key press events up to it's parent when the drop-down selection window is open. However, an Enter key press will still be "unconsumed" when the drop-down window is not open. When set to false, this method will always pass the Enter key press up to it's parent.

      The default is true. Clients will set this to false when they wish to respond to an Enter event. For example, a dialog may want to close itself on an Enter key press, even when the drop-down selection text field is still open. Contrastingly, when this field is embedded inside of a larger editor, like a multi-editor field dialog, the Enter key press should simply trigger the drop-down window to close and the editing to stop, but should not trigger the overall dialog to close.

      Parameters:
      consume - true to consume
    • setIgnoreEnterKeyPress

      public void setIgnoreEnterKeyPress(boolean ignore)
      True signals to do nothing when the user presses Enter. The default is to respond to the Enter key, using any existing selection to set this field's selected value.

      This can be set to true to allow clients to show drop-down matches without allowing the user to select them, triggering the window to be closed.

      Parameters:
      ignore - true to ignore Enter presses; false is the default
    • setMatchingWindowHeight

      public void setMatchingWindowHeight(int height)
      Sets the height of the matching window. The default value is 300.
      Parameters:
      height - the new height
    • addDropDownSelectionChoiceListener

      public void addDropDownSelectionChoiceListener(DropDownSelectionChoiceListener<T> listener)
      Adds a listener that will be called back when the user makes a choice from the drop-down list. A choice is a user action that triggers the selection window to be closed and updates the text field.

      Note: the listener is stored in a weak data structure, so you must maintain a reference to the listener you pass in--anonymous classes or lambdas will not work.

      Parameters:
      listener - the listener
    • addCellEditorListener

      public void addCellEditorListener(CellEditorListener listener)
      Adds a listener to be notified when cell editing is canceled or completed.
      Parameters:
      listener - The listener to add
      Throws:
      IllegalArgumentException - if the listener has already been added
    • removeCellEditorListener

      public void removeCellEditorListener(CellEditorListener listener)
      Removes the given listener from this class if it has previously been added.
      Parameters:
      listener - The listener to remove.
    • setTextFromList

      protected void setTextFromList()
    • shouldReplaceTextFieldTextWithSelectedItem

      protected boolean shouldReplaceTextFieldTextWithSelectedItem(String textFieldText, T selectedItem)
    • getSelectedValue

      public T getSelectedValue()
      Returns the user's selection or null if the user has not made a selection.

      Note: the the value returned from this method may not match the text in the field in the case that the user has selected a value and then typed some text.

      Returns:
      the user's selection or null if the user has not made a selection.
    • setSelectedValue

      public void setSelectedValue(T value)
      Sets the current selection on this text field. This will store the provided value and set the text of the text field to be the name of that value. If the given value is null, then the text of this field will be cleared.
      Parameters:
      value - The value that is to be the current selection or null to clear the selected value of this text field.
    • closeDropDownWindow

      public void closeDropDownWindow()
      Closes the drop down window
    • hideMatchingWindow

      protected void hideMatchingWindow()
    • setTextFromSelectedListItemAndKeepMatchingWindowOpen

      protected void setTextFromSelectedListItemAndKeepMatchingWindowOpen()