Class SuggestBox
- All Implemented Interfaces:
IsEditor<LeafValueEditor<String>>
,HasAllKeyHandlers
,HasKeyDownHandlers
,HasKeyPressHandlers
,HasKeyUpHandlers
,HasAttachHandlers
,HasSelectionHandlers<SuggestOracle.Suggestion>
,HasValueChangeHandlers<String>
,HasHandlers
,EventListener
,TakesValue<String>
,FiresSuggestionEvents
,Focusable
,HasAnimation
,HasEnabled
,HasFocus
,HasText
,HasValue<String>
,HasVisibility
,IsRenderable
,IsWidget
,SourcesChangeEvents
,SourcesClickEvents
,SourcesFocusEvents
,SourcesKeyboardEvents
SuggestBox
is a text box or text area which displays a
pre-configured set of selections that match the user's input.
Each SuggestBox
is associated with a single SuggestOracle
.
The SuggestOracle
is used to provide a set of selections given a
specific query string.
By default, the SuggestBox
uses a MultiWordSuggestOracle
as
its oracle. Below we show how a MultiWordSuggestOracle
can be
configured:
MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(); oracle.add("Cat"); oracle.add("Dog"); oracle.add("Horse"); oracle.add("Canary"); SuggestBox box = new SuggestBox(oracle);Using the example above, if the user types "C" into the text widget, the oracle will configure the suggestions with the "Cat" and "Canary" suggestions. Specifically, whenever the user types a key into the text widget, the value is submitted to the
MultiWordSuggestOracle
.
Note that there is no method to retrieve the "currently selected suggestion"
in a SuggestBox, because there are points in time where the currently
selected suggestion is not defined. For example, if the user types in some
text that does not match any of the SuggestBox's suggestions, then the
SuggestBox will not have a currently selected suggestion. It is more useful
to know when a suggestion has been chosen from the SuggestBox's list of
suggestions. A SuggestBox fires SelectionEvents
whenever a suggestion is chosen, and handlers for these events can be added
using the addSelectionHandler(SelectionHandler)
method.
CSS Style Rules
- .gwt-SuggestBox
- the suggest box itself
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
The default implementation ofSuggestBox.SuggestionDisplay
displays suggestions in aPopupPanel
beneath theSuggestBox
.static interface
The callback used when a user selects aSuggestOracle.Suggestion
.static class
Used to display suggestions to the user.Nested classes/interfaces inherited from class com.google.gwt.user.client.ui.UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled
-
Field Summary
Fields inherited from class com.google.gwt.user.client.ui.Widget
eventsToSink
Fields inherited from class com.google.gwt.user.client.ui.UIObject
DEBUG_ID_PREFIX, MISSING_ELEMENT_ERROR, SETELEMENT_TWICE_ERROR
-
Constructor Summary
ConstructorDescriptionConstructor forSuggestBox
.SuggestBox
(SuggestOracle oracle) Constructor forSuggestBox
.SuggestBox
(SuggestOracle oracle, ValueBoxBase<String> box) Constructor forSuggestBox
.SuggestBox
(SuggestOracle oracle, ValueBoxBase<String> box, SuggestBox.SuggestionDisplay suggestDisplay) Constructor forSuggestBox
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addChangeListener
(ChangeListener listener) Deprecated.void
addClickListener
(ClickListener listener) Deprecated.usegetTextBox()
().addClickHandler insteadvoid
addEventHandler
(SuggestionHandler handler) void
addFocusListener
(FocusListener listener) Deprecated.usegetTextBox()
().addFocusHandler/addBlurHandler() insteadvoid
addKeyboardListener
(KeyboardListener listener) addKeyDownHandler
(KeyDownHandler handler) Adds aKeyDownEvent
handler.addKeyPressHandler
(KeyPressHandler handler) Adds aKeyPressEvent
handler.addKeyUpHandler
(KeyUpHandler handler) Adds aKeyUpEvent
handler.Adds aSelectionEvent
handler.addValueChangeHandler
(ValueChangeHandler<String> handler) Adds aValueChangeEvent
handler.asEditor()
Returns aTakesValueEditor
backed by the SuggestBox.int
getLimit()
Gets the limit for the number of suggestions that should be displayed for this box.Get theSuggestBox.SuggestionDisplay
used to display suggestions.Gets the suggest box'sSuggestOracle
.int
Gets the widget's position in the tab index.getText()
Gets this object's text.Deprecated.in favour of getValueBoxgetValue()
Gets this object's value.Get the ValueBoxBase associated with this suggest box.void
Deprecated.boolean
Deprecated.boolean
Returns whether or not the first suggestion will be automatically selected.boolean
Gets whether this widget is enabled.boolean
Check if theSuggestBox.SuggestionDisplay
is showing.protected void
onEnsureDebugId
(String baseID) Called when the user sets the id using theUIObject.ensureDebugId(String)
method.void
Refreshes the current list of suggestions.void
removeChangeListener
(ChangeListener listener) Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().addChangeHandler insteadvoid
removeClickListener
(ClickListener listener) Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().addClickHandler insteadvoid
removeEventHandler
(SuggestionHandler handler) Deprecated.Use theHandlerRegistration.removeHandler()
method no the object returned byaddSelectionHandler(com.google.gwt.event.logical.shared.SelectionHandler<com.google.gwt.user.client.ui.SuggestOracle.Suggestion>)
insteadvoid
removeFocusListener
(FocusListener listener) Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().addFocusListener insteadvoid
removeKeyboardListener
(KeyboardListener listener) Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().add*Handler insteadvoid
setAccessKey
(char key) Sets the widget's 'access key'.void
setAnimationEnabled
(boolean enable) Deprecated.void
setAutoSelectEnabled
(boolean selectsFirstItem) Turns on or off the behavior that automatically selects the first suggested item.void
setEnabled
(boolean enabled) Sets whether this widget is enabled.void
setFocus
(boolean focused) Explicitly focus/unfocus this widget.void
setLimit
(int limit) Sets the limit to the number of suggestions the oracle should provide.void
setPopupStyleName
(String style) Deprecated.void
setTabIndex
(int index) Sets the widget's position in the tab index.void
Sets this object's text.void
Sets this object's value without firing any events.void
Sets this object's value.void
Show the current list of suggestions.(package private) void
showSuggestions
(String query) static SuggestBox
wrap
(SuggestOracle oracle, Element element) Creates aSuggestBox
widget that wraps an existing <input type='text'> element.Methods inherited from class com.google.gwt.user.client.ui.Composite
claimElement, getWidget, initializeClaimedElement, initWidget, isAttached, onAttach, onBrowserEvent, onDetach, render, render, resolvePotentialElement, setWidget
Methods inherited from class com.google.gwt.user.client.ui.Widget
addAttachHandler, addBitlessDomHandler, addDomHandler, addHandler, asWidget, asWidgetOrNull, createHandlerManager, delegateEvent, doAttachChildren, doDetachChildren, ensureHandlers, fireEvent, getHandlerCount, getHandlerManager, getLayoutData, getParent, isOrWasAttached, onLoad, onUnload, removeFromParent, replaceElement, setLayoutData, setParent, sinkEvents, unsinkEvents
Methods inherited from class com.google.gwt.user.client.ui.UIObject
addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, removeStyleDependentName, removeStyleName, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.google.gwt.event.shared.HasHandlers
fireEvent
-
Constructor Details
-
SuggestBox
public SuggestBox()Constructor forSuggestBox
. Creates aMultiWordSuggestOracle
andTextBox
to use with thisSuggestBox
. -
SuggestBox
- Parameters:
oracle
- the oracle for thisSuggestBox
-
SuggestBox
Constructor forSuggestBox
. The text box will be removed from it's current location and wrapped by theSuggestBox
.- Parameters:
oracle
- supplies suggestions based upon the current contents of the text widgetbox
- the text widget
-
SuggestBox
public SuggestBox(SuggestOracle oracle, ValueBoxBase<String> box, SuggestBox.SuggestionDisplay suggestDisplay) Constructor forSuggestBox
. The text box will be removed from it's current location and wrapped by theSuggestBox
.- Parameters:
oracle
- supplies suggestions based upon the current contents of the text widgetbox
- the text widgetsuggestDisplay
- the class used to display suggestions
-
-
Method Details
-
wrap
Creates aSuggestBox
widget that wraps an existing <input type='text'> element. This element must already be attached to the document. If the element is removed from the document, you must callWidget.removeFromParent()
.- Parameters:
oracle
- the suggest box oracle to useelement
- the element to be wrapped
-
addChangeListener
Deprecated.usegetTextBox()
().addChangeHandler insteadAdds a listener to receive change events on the SuggestBox's text box. The source Widget for these events will be the SuggestBox.- Specified by:
addChangeListener
in interfaceSourcesChangeEvents
- Parameters:
listener
- the listener interface to add
-
addClickListener
Deprecated.usegetTextBox()
().addClickHandler insteadAdds a listener to receive click events on the SuggestBox's text box. The source Widget for these events will be the SuggestBox.- Specified by:
addClickListener
in interfaceSourcesClickEvents
- Parameters:
listener
- the listener interface to add
-
addEventHandler
Deprecated.Adds an event to this handler.- Specified by:
addEventHandler
in interfaceFiresSuggestionEvents
- Parameters:
handler
- the handler to add
-
addFocusListener
Deprecated.usegetTextBox()
().addFocusHandler/addBlurHandler() insteadAdds a listener to receive focus events on the SuggestBox's text box. The source Widget for these events will be the SuggestBox.- Specified by:
addFocusListener
in interfaceSourcesFocusEvents
- Parameters:
listener
- the listener interface to add
-
addKeyboardListener
Deprecated.Description copied from interface:SourcesKeyboardEvents
Adds a listener interface to receive keyboard events.- Specified by:
addKeyboardListener
in interfaceSourcesKeyboardEvents
- Parameters:
listener
- the listener interface to add
-
addKeyDownHandler
Description copied from interface:HasKeyDownHandlers
Adds aKeyDownEvent
handler.- Specified by:
addKeyDownHandler
in interfaceHasKeyDownHandlers
- Parameters:
handler
- the key down handler- Returns:
HandlerRegistration
used to remove this handler
-
addKeyPressHandler
Description copied from interface:HasKeyPressHandlers
Adds aKeyPressEvent
handler.- Specified by:
addKeyPressHandler
in interfaceHasKeyPressHandlers
- Parameters:
handler
- the key press handler- Returns:
HandlerRegistration
used to remove this handler
-
addKeyUpHandler
Description copied from interface:HasKeyUpHandlers
Adds aKeyUpEvent
handler.- Specified by:
addKeyUpHandler
in interfaceHasKeyUpHandlers
- Parameters:
handler
- the key up handler- Returns:
HandlerRegistration
used to remove this handler
-
asEditor
Returns aTakesValueEditor
backed by the SuggestBox.- Specified by:
asEditor
in interfaceIsEditor<LeafValueEditor<String>>
- Returns:
- an
Editor
of type E
-
getLimit
public int getLimit()Gets the limit for the number of suggestions that should be displayed for this box. It is up to the currentSuggestOracle
to enforce this limit.- Returns:
- the limit for the number of suggestions
-
getSuggestionDisplay
Get theSuggestBox.SuggestionDisplay
used to display suggestions.- Returns:
- the
SuggestBox.SuggestionDisplay
-
getSuggestOracle
Gets the suggest box'sSuggestOracle
.- Returns:
- the
SuggestOracle
-
getTabIndex
public int getTabIndex()Description copied from interface:Focusable
Gets the widget's position in the tab index.- Specified by:
getTabIndex
in interfaceFocusable
- Returns:
- the widget's tab index
-
getText
Description copied from interface:HasText
Gets this object's text. -
getTextBox
Deprecated.in favour of getValueBoxGet the text box associated with this suggest box.- Returns:
- this suggest box's text box
- Throws:
ClassCastException
- if this suggest box's value box is not an instance of TextBoxBase
-
getValue
Description copied from interface:HasValue
Gets this object's value. -
getValueBox
Get the ValueBoxBase associated with this suggest box.- Returns:
- this suggest box's value box
-
hideSuggestionList
Deprecated.Hide current suggestions in theSuggestBox.DefaultSuggestionDisplay
. Note that this method is a no-op unless theSuggestBox.DefaultSuggestionDisplay
is used. -
isAnimationEnabled
Deprecated.Check whether or not theSuggestBox.DefaultSuggestionDisplay
has animations enabled. Note that this method only has a meaningful return value when theSuggestBox.DefaultSuggestionDisplay
is used.- Specified by:
isAnimationEnabled
in interfaceHasAnimation
-
isAutoSelectEnabled
public boolean isAutoSelectEnabled()Returns whether or not the first suggestion will be automatically selected. This behavior is on by default.- Returns:
- true if the first suggestion will be automatically selected
-
isEnabled
public boolean isEnabled()Gets whether this widget is enabled.- Specified by:
isEnabled
in interfaceHasEnabled
- Returns:
true
if the widget is enabled
-
isSuggestionListShowing
public boolean isSuggestionListShowing()Check if theSuggestBox.SuggestionDisplay
is showing.- Returns:
- true if the list of suggestions is currently showing, false if not
-
refreshSuggestionList
public void refreshSuggestionList()Refreshes the current list of suggestions. -
removeChangeListener
Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().addChangeHandler insteadDescription copied from interface:SourcesChangeEvents
Removes a previously added listener interface.- Specified by:
removeChangeListener
in interfaceSourcesChangeEvents
- Parameters:
listener
- the listener interface to remove
-
removeClickListener
Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().addClickHandler insteadDescription copied from interface:SourcesClickEvents
Removes a previously added listener interface.- Specified by:
removeClickListener
in interfaceSourcesClickEvents
- Parameters:
listener
- the listener interface to remove
-
removeEventHandler
Deprecated.Use theHandlerRegistration.removeHandler()
method no the object returned byaddSelectionHandler(com.google.gwt.event.logical.shared.SelectionHandler<com.google.gwt.user.client.ui.SuggestOracle.Suggestion>)
insteadDescription copied from interface:FiresSuggestionEvents
Removes a previously added handler interface.- Specified by:
removeEventHandler
in interfaceFiresSuggestionEvents
- Parameters:
handler
- the handler to remove
-
removeFocusListener
Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().addFocusListener insteadDescription copied from interface:SourcesFocusEvents
Removes a previously added listener interface.- Specified by:
removeFocusListener
in interfaceSourcesFocusEvents
- Parameters:
listener
- the listener interface to remove
-
removeKeyboardListener
Deprecated.Use theHandlerRegistration.removeHandler()
method on the object returned bygetTextBox()
().add*Handler insteadDescription copied from interface:SourcesKeyboardEvents
Removes a previously added listener interface.- Specified by:
removeKeyboardListener
in interfaceSourcesKeyboardEvents
- Parameters:
listener
- the listener interface to remove
-
setAccessKey
public void setAccessKey(char key) Description copied from interface:Focusable
Sets the widget's 'access key'. This key is used (in conjunction with a browser-specific modifier key) to automatically focus the widget.- Specified by:
setAccessKey
in interfaceFocusable
- Parameters:
key
- the widget's access key
-
setAnimationEnabled
Deprecated.Enable or disable animations in theSuggestBox.DefaultSuggestionDisplay
. Note that this method is a no-op unless theSuggestBox.DefaultSuggestionDisplay
is used.- Specified by:
setAnimationEnabled
in interfaceHasAnimation
- Parameters:
enable
- true to enable, false to disable
-
setAutoSelectEnabled
public void setAutoSelectEnabled(boolean selectsFirstItem) Turns on or off the behavior that automatically selects the first suggested item. This behavior is on by default.- Parameters:
selectsFirstItem
- Whether or not to automatically select the first suggestion
-
setEnabled
public void setEnabled(boolean enabled) Sets whether this widget is enabled.- Specified by:
setEnabled
in interfaceHasEnabled
- Parameters:
enabled
-true
to enable the widget,false
to disable it
-
setFocus
public void setFocus(boolean focused) Description copied from interface:Focusable
Explicitly focus/unfocus this widget. Only one widget can have focus at a time, and the widget that does will receive all keyboard events. NOTE: Most browsers fire FocusEvents asynchronously. Especially within GWT tests, you'll need to make your test asynchronous to properly do verifications. SeeGWTTestCase#delayTestFinish
for more information on how to do this. -
setLimit
public void setLimit(int limit) Sets the limit to the number of suggestions the oracle should provide. It is up to the oracle to enforce this limit.- Parameters:
limit
- the limit to the number of suggestions provided
-
setPopupStyleName
Deprecated.Sets the style name of the suggestion popup in theSuggestBox.DefaultSuggestionDisplay
. Note that this method is a no-op unless theSuggestBox.DefaultSuggestionDisplay
is used.- Parameters:
style
- the new primary style name- See Also:
-
setTabIndex
public void setTabIndex(int index) Description copied from interface:Focusable
Sets the widget's position in the tab index. If more than one widget has the same tab index, each such widget will receive focus in an arbitrary order. Setting the tab index to-1
will cause this widget to be removed from the tab order.- Specified by:
setTabIndex
in interfaceFocusable
- Parameters:
index
- the widget's tab index
-
setText
Description copied from interface:HasText
Sets this object's text. -
setValue
Description copied from interface:HasValue
Sets this object's value without firing any events. This should be identical to calling setValue(value, false).It is acceptable to fail assertions or throw (documented) unchecked exceptions in response to bad values.
Widgets must accept null as a valid value. By convention, setting a widget to null clears value, calling getValue() on a cleared widget returns null. Widgets that can not be cleared (e.g.
CheckBox
) must find another valid meaning for null input. -
setValue
Description copied from interface:HasValue
Sets this object's value. FiresValueChangeEvent
when fireEvents is true and the new value does not equal the existing value.It is acceptable to fail assertions or throw (documented) unchecked exceptions in response to bad values.
-
showSuggestionList
public void showSuggestionList()Show the current list of suggestions. -
onEnsureDebugId
Description copied from class:UIObject
Called when the user sets the id using theUIObject.ensureDebugId(String)
method. Subclasses ofUIObject
can override this method to add IDs to their sub elements. If a subclass does override this method, it should list the IDs (relative to the base ID), that will be applied to each subElement
with a short description. For example:- -mysubelement = Applies to my sub element.
<inherits name="com.google.gwt.user.Debug"/>
- Overrides:
onEnsureDebugId
in classUIObject
- Parameters:
baseID
- the base ID used by the main element
-
showSuggestions
-
getTextBox()
().addChangeHandler instead