Class AutoCompletion

java.lang.Object
com.jidesoft.swing.AutoCompletion

public class AutoCompletion extends Object
AutoCompletion is a helper class to make JTextComponent or JComboBox auto-complete based on a list of known items.

There are three constructors. The simplest one is AutoCompletion(javax.swing.JComboBox). It takes any combobox and make it auto completion. If you are looking for an auto-complete combobox solution, this is all you need. However AutoCompletion can do more than that. There are two more constructors. One is AutoCompletion(javax.swing.text.JTextComponent, Searchable). It will use Searchable which is another component available in JIDE to make the JTextCompoent auto-complete. We used Searchable here because it provides a common interface to access the element in JTree, JList or JTable. In the other word, the known list item we used to auto-complete can be got from JTree or JList or even JTable or any other component as long as it has Searchable interface implemented. The last constructor takes any java.util.List and use it as auto completion list.

The only option available on AutoCompletion is setStrict(boolean). If it's true, it will not allow user to type in anything that is not in the known item list. If false, user can type in whatever he/she wants. If the text can match with a item in the known item list, it will still auto-complete.

Author:
Thomas Bierhance, JIDE Software, Inc.
  • Field Details

    • CLIENT_PROPERTY_AUTO_COMPLETION

      public static final String CLIENT_PROPERTY_AUTO_COMPLETION
      The client property for AutoCompletion instance. When AutoCompletion is installed on a text component, this client property has the AutoCompletion.
      See Also:
  • Constructor Details

  • Method Details

    • uninstallListeners

      public void uninstallListeners()
      Uninstalls the listeners so that the component is not auto-completion anymore.
    • installListeners

      public void installListeners()
      Installs the listeners needed for auto-completion feature. Please note, this method is already called when you create AutoCompletion. Unless you called uninstallListeners(), there is no need to call this method yourself.
    • createDocument

      protected AutoCompletion.AutoCompletionDocument createDocument()
      Creates AutoCompletionDocument.
      Returns:
      the AutoCompletionDocument.
    • isStrict

      public boolean isStrict()
      Gets the strict property.
      Returns:
      the value of strict property.
    • setStrict

      public void setStrict(boolean strict)
      Sets the strict property. If true, it will not allow user to type in anything that is not in the known item list. If false, user can type in whatever he/she wants. If the text can match with a item in the known item list, it will still auto-complete.
      Parameters:
      strict -
    • isStrictCompletion

      public boolean isStrictCompletion()
      Gets the strict completion property.
      Returns:
      the value of strict completion property.
      See Also:
    • setStrictCompletion

      public void setStrictCompletion(boolean strictCompletion)
      Sets the strict completion property. If true, in case insensitive searching, it will always use the exact item in the Searchable to replace whatever user types. For example, when Searchable has an item "Arial" and user types in "AR", if this flag is true, it will auto-completed as "Arial". If false, it will be auto-completed as "ARial". Of course, this flag will only make a difference if Searchable is case insensitive.
      Parameters:
      strictCompletion -
    • getTextComponent

      protected JTextComponent getTextComponent()
      Gets the underlying text component which auto-completes.
      Returns:
      the underlying text component.
    • getSearchable

      public Searchable getSearchable()
      Gets the underlying Searchable. If you use the constructor AutoCompletion(javax.swing.text.JTextComponent, Searchable), the return value will be the Searchable you passed in. If you use the other twoconstructorss, internally we will still create a Searchable. If so, this Searchable will be returned.
      Returns:
      the Searchable.
    • getAutoCompletion

      public static AutoCompletion getAutoCompletion(JComponent component)
      When auto-completion is enabled on a text component, we will set a client property on it. This method will look for this client property and return you the instance of the AutoCompletion that is installed on the component.
      Parameters:
      component - the component.
      Returns:
      the AutoCompletion. If null, it means there is no AutoCompletion installed.