Package com.jidesoft.swing
Class AutoCompletion
- java.lang.Object
-
- com.jidesoft.swing.AutoCompletion
-
public class AutoCompletion extends java.lang.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 isAutoCompletion(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. HoweverAutoCompletion
can do more than that. There are two more constructors. One isAutoCompletion(javax.swing.text.JTextComponent, Searchable)
. It will useSearchable
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 onAutoCompletion
issetStrict(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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AutoCompletion.AutoCompletionDocument
The document class used by AutoCompletion.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CLIENT_PROPERTY_AUTO_COMPLETION
The client property for AutoCompletion instance.
-
Constructor Summary
Constructors Constructor Description AutoCompletion(javax.swing.JComboBox comboBox)
AutoCompletion(javax.swing.JComboBox comboBox, Searchable searchable)
AutoCompletion(javax.swing.text.JTextComponent textComponent, Searchable searchable)
AutoCompletion(javax.swing.text.JTextComponent textComponent, java.lang.Object[] array)
AutoCompletion(javax.swing.text.JTextComponent textComponent, java.util.List list)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected AutoCompletion.AutoCompletionDocument
createDocument()
Creates AutoCompletionDocument.static AutoCompletion
getAutoCompletion(javax.swing.JComponent component)
When auto-completion is enabled on a text component, we will set a client property on it.Searchable
getSearchable()
Gets the underlying Searchable.protected javax.swing.text.JTextComponent
getTextComponent()
Gets the underlying text component which auto-completes.void
installListeners()
Installs the listeners needed for auto-completion feature.boolean
isStrict()
Gets the strict property.boolean
isStrictCompletion()
Gets the strict completion property.void
setStrict(boolean strict)
Sets the strict property.void
setStrictCompletion(boolean strictCompletion)
Sets the strict completion property.void
uninstallListeners()
Uninstalls the listeners so that the component is not auto-completion anymore.
-
-
-
Field Detail
-
CLIENT_PROPERTY_AUTO_COMPLETION
public static final java.lang.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:
- Constant Field Values
-
-
Constructor Detail
-
AutoCompletion
public AutoCompletion(javax.swing.JComboBox comboBox)
-
AutoCompletion
public AutoCompletion(javax.swing.JComboBox comboBox, Searchable searchable)
-
AutoCompletion
public AutoCompletion(javax.swing.text.JTextComponent textComponent, Searchable searchable)
-
AutoCompletion
public AutoCompletion(javax.swing.text.JTextComponent textComponent, java.util.List list)
-
AutoCompletion
public AutoCompletion(javax.swing.text.JTextComponent textComponent, java.lang.Object[] array)
-
-
Method Detail
-
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 calleduninstallListeners()
, 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(boolean)
-
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 javax.swing.text.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 constructorAutoCompletion(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(javax.swing.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.
-
-