Package com.google.gwt.user.client.ui
Class CheckBox
java.lang.Object
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.FocusWidget
com.google.gwt.user.client.ui.ButtonBase
com.google.gwt.user.client.ui.CheckBox
- All Implemented Interfaces:
IsEditor<LeafValueEditor<Boolean>>,HasAllDragAndDropHandlers,HasAllFocusHandlers,HasAllGestureHandlers,HasAllKeyHandlers,HasAllMouseHandlers,HasAllTouchHandlers,HasBlurHandlers,HasClickHandlers,HasDoubleClickHandlers,HasDragEndHandlers,HasDragEnterHandlers,HasDragHandlers,HasDragLeaveHandlers,HasDragOverHandlers,HasDragStartHandlers,HasDropHandlers,HasFocusHandlers,HasGestureChangeHandlers,HasGestureEndHandlers,HasGestureStartHandlers,HasKeyDownHandlers,HasKeyPressHandlers,HasKeyUpHandlers,HasMouseDownHandlers,HasMouseMoveHandlers,HasMouseOutHandlers,HasMouseOverHandlers,HasMouseUpHandlers,HasMouseWheelHandlers,HasTouchCancelHandlers,HasTouchEndHandlers,HasTouchMoveHandlers,HasTouchStartHandlers,HasAttachHandlers,HasValueChangeHandlers<Boolean>,HasHandlers,HasDirectionEstimator,HasSafeHtml,EventListener,TakesValue<Boolean>,Focusable,HasDirectionalSafeHtml,HasDirectionalText,HasEnabled,HasFocus,HasHTML,HasName,HasText,HasValue<Boolean>,HasVisibility,HasWordWrap,IsWidget,SourcesClickEvents,SourcesFocusEvents,SourcesKeyboardEvents,SourcesMouseEvents
- Direct Known Subclasses:
RadioButton
public class CheckBox
extends ButtonBase
implements HasName, HasValue<Boolean>, HasWordWrap, HasDirectionalSafeHtml, HasDirectionEstimator, IsEditor<LeafValueEditor<Boolean>>
A standard check box widget.
This class also serves as a base class for
RadioButton.
Built-in Bidi Text Support
This widget is capable of automatically adjusting its direction according to its content. This feature is controlled bysetDirectionEstimator(boolean) or
passing a DirectionEstimator parameter to the constructor, and is off by
default.
CSS Style Rules
- .gwt-CheckBox
- the outer element
- .gwt-CheckBox-disabled
- applied when Checkbox is disabled
Example
public class CheckBoxExample implements EntryPoint {
@Override
public void onModuleLoad() {
// Make a new check box, and select it by default.
CheckBox cb = new CheckBox("Foo");
cb.setValue(true);
// Hook up a handler to find out when it's clicked.
cb.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
boolean checked = ((CheckBox) event.getSource()).getValue();
Window.alert("It is " + (checked ? "" : "not ") + "checked");
}
});
// Add it to the root panel.
RootPanel.get().add(cb);
}
}
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.gwt.user.client.ui.UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DirectionEstimator(package private) final DirectionalTextHelper(package private) InputElement(package private) LabelElementFields inherited from class com.google.gwt.user.client.ui.Widget
eventsToSinkFields inherited from class com.google.gwt.user.client.ui.UIObject
DEBUG_ID_PREFIX, MISSING_ELEMENT_ERROR, SETELEMENT_TWICE_ERROR -
Constructor Summary
ConstructorsModifierConstructorDescriptionCheckBox()Creates a check box with no label.protectedCreates a check box with the specified text label.CheckBox(SafeHtml label, HasDirection.Direction dir) Creates a check box with the specified text label.CheckBox(SafeHtml label, DirectionEstimator directionEstimator) Creates a check box with the specified text label.Creates a check box with the specified text label.Creates a check box with the specified text label.CheckBox(String label, HasDirection.Direction dir) Creates a check box with the specified text label.CheckBox(String label, DirectionEstimator directionEstimator) Creates a label with the specified text and a default direction estimator. -
Method Summary
Modifier and TypeMethodDescriptionaddValueChangeHandler(ValueChangeHandler<Boolean> handler) Adds aValueChangeEventhandler.asEditor()Returns the Editor encapsulated by the view object.protected voidReturns theDirectionEstimatorobject.Returns the value property of the input element that backs this widget.getHTML()Gets this object's contents as HTML.getName()Gets the widget's name.intGets the tab index.getText()Gets this object's text.Gets the direction of this object's text.getValue()Determines whether this check box is currently checked.booleanGets whether word-wrapping is enabled.booleanDeprecated.booleanGets whether this widget is enabled.protected voidonEnsureDebugId(String baseID) Affected Elements: -label = label next to checkbox.protected voidonLoad()This method is called when a widget is attached to the browser's document.protected voidonUnload()This method is called when a widget is detached from the browser's document.protected voidreplaceInputElement(Element elem) Replace the current input element with a new one.protected voidreplaceInputElement(Element elem) Deprecated.Call and usereplaceInputElement(com.google.gwt.dom.client.Element)instead.voidsetAccessKey(char key) Sets the widget's 'access key'.voidsetChecked(boolean checked) Deprecated.UsesetValue(Boolean)insteadvoidsetDirectionEstimator(boolean enabled) Toggles on / off direction estimation.voidsetDirectionEstimator(DirectionEstimator directionEstimator) Sets theDirectionEstimatorobject.voidsetEnabled(boolean enabled) Sets whether this widget is enabled.voidsetFocus(boolean focused) Explicitly focus/unfocus this widget.voidsetFormValue(String value) Set the value property on the input element that backs this widget.voidsetHTML(SafeHtml html, HasDirection.Direction dir) Sets this object's html, also declaring its direction.voidSets this object's contents via HTML.voidSets the widget's name.voidsetTabIndex(int index) Sets the widget's position in the tab index.voidSets this object's text.voidsetText(String text, HasDirection.Direction dir) Sets this object's text, also declaring its direction.voidChecks or unchecks the check box.voidChecks or unchecks the check box, firingValueChangeEventif appropriate.voidsetWordWrap(boolean wrap) Sets whether word-wrapping is enabled.voidsinkEvents(int eventBitsToAdd) Overridden to defer the call to super.sinkEvents until the first time this widget is attached to the dom, as a performance enhancement.Methods inherited from class com.google.gwt.user.client.ui.ButtonBase
setHTMLMethods inherited from class com.google.gwt.user.client.ui.FocusWidget
addBlurHandler, addClickHandler, addClickListener, addDoubleClickHandler, addDragEndHandler, addDragEnterHandler, addDragHandler, addDragLeaveHandler, addDragOverHandler, addDragStartHandler, addDropHandler, addFocusHandler, addFocusListener, addGestureChangeHandler, addGestureEndHandler, addGestureStartHandler, addKeyboardListener, addKeyDownHandler, addKeyPressHandler, addKeyUpHandler, addMouseDownHandler, addMouseListener, addMouseMoveHandler, addMouseOutHandler, addMouseOverHandler, addMouseUpHandler, addMouseWheelHandler, addMouseWheelListener, addTouchCancelHandler, addTouchEndHandler, addTouchMoveHandler, addTouchStartHandler, getFocusImpl, onAttach, removeClickListener, removeFocusListener, removeKeyboardListener, removeMouseListener, removeMouseWheelListenerMethods 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, isAttached, isOrWasAttached, onBrowserEvent, onDetach, removeFromParent, replaceElement, setLayoutData, setParent, unsinkEventsMethods 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, resolvePotentialElement, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.google.gwt.event.shared.HasHandlers
fireEventMethods inherited from interface com.google.gwt.safehtml.client.HasSafeHtml
setHTML
-
Field Details
-
DEFAULT_DIRECTION_ESTIMATOR
-
directionalTextHelper
-
inputElem
InputElement inputElem -
labelElem
LabelElement labelElem
-
-
Constructor Details
-
CheckBox
public CheckBox()Creates a check box with no label. -
CheckBox
Creates a check box with the specified text label.- Parameters:
label- the check box's label
-
CheckBox
Creates a check box with the specified text label.- Parameters:
label- the check box's labeldir- the text's direction. Note thatDEFAULTmeans direction should be inherited from the widget's parent element.
-
CheckBox
Creates a check box with the specified text label.- Parameters:
label- the check box's labelasHTML-trueto treat the specified label as html
-
CheckBox
-
-
Method Details
-
asEditor
Description copied from interface:IsEditorReturns the Editor encapsulated by the view object.- Specified by:
asEditorin interfaceIsEditor<LeafValueEditor<Boolean>>- Returns:
- an
Editorof type E
-
getDirectionEstimator
Description copied from interface:HasDirectionEstimatorReturns theDirectionEstimatorobject.- Specified by:
getDirectionEstimatorin interfaceHasDirectionEstimator
-
getFormValue
Returns the value property of the input element that backs this widget. This is the value that will be associated with the CheckBox name and submitted to the server if aFormPanelthat holds it is submitted and the box is checked.Don't confuse this with
getValue(), which returns true or false if the widget is checked. -
getHTML
Description copied from interface:HasHTMLGets this object's contents as HTML.- Specified by:
getHTMLin interfaceHasHTML- Overrides:
getHTMLin classButtonBase- Returns:
- the object's HTML
-
getName
Description copied from interface:HasNameGets the widget's name. -
getTabIndex
public int getTabIndex()Description copied from class:FocusWidgetGets the tab index.- Specified by:
getTabIndexin interfaceFocusable- Overrides:
getTabIndexin classFocusWidget- Returns:
- the tab index
-
getText
Description copied from interface:HasTextGets this object's text.- Specified by:
getTextin interfaceHasText- Overrides:
getTextin classButtonBase- Returns:
- the object's text
-
getTextDirection
Description copied from interface:HasDirectionalTextGets the direction of this object's text.- Specified by:
getTextDirectionin interfaceHasDirectionalText- Returns:
- the direction of this object's text
-
getValue
Determines whether this check box is currently checked.Note that this does not return the value property of the checkbox input element wrapped by this widget. For access to that property, see
getFormValue() -
getWordWrap
public boolean getWordWrap()Description copied from interface:HasWordWrapGets whether word-wrapping is enabled.- Specified by:
getWordWrapin interfaceHasWordWrap- Returns:
trueif word-wrapping is enabled.
-
isChecked
Deprecated.UsegetValue()insteadDetermines whether this check box is currently checked.- Returns:
trueif the check box is checked
-
isEnabled
public boolean isEnabled()Description copied from class:FocusWidgetGets whether this widget is enabled.- Specified by:
isEnabledin interfaceHasEnabled- Overrides:
isEnabledin classFocusWidget- Returns:
trueif the widget is enabled
-
setAccessKey
public void setAccessKey(char key) Description copied from interface:FocusableSets the widget's 'access key'. This key is used (in conjunction with a browser-specific modifier key) to automatically focus the widget.- Specified by:
setAccessKeyin interfaceFocusable- Overrides:
setAccessKeyin classFocusWidget- Parameters:
key- the widget's access key
-
setChecked
Deprecated.UsesetValue(Boolean)insteadChecks or unchecks this check box. Does not fireValueChangeEvent. (If you want the event to fire, usesetValue(Boolean, boolean))- Parameters:
checked-trueto check the check box.
-
setDirectionEstimator
public void setDirectionEstimator(boolean enabled) Toggles on / off direction estimation.See note at
setDirectionEstimator(DirectionEstimator).- Specified by:
setDirectionEstimatorin interfaceHasDirectionEstimator- Parameters:
enabled- Whether to enable direction estimation. Iftrue, sets theDirectionEstimatorobject to a defaultDirectionEstimator.
-
setEnabled
public void setEnabled(boolean enabled) Description copied from class:FocusWidgetSets whether this widget is enabled.- Specified by:
setEnabledin interfaceHasEnabled- Overrides:
setEnabledin classFocusWidget- Parameters:
enabled-trueto enable the widget,falseto disable it
-
setFocus
public void setFocus(boolean focused) Description copied from interface:FocusableExplicitly 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#delayTestFinishfor more information on how to do this.- Specified by:
setFocusin interfaceFocusable- Overrides:
setFocusin classFocusWidget- Parameters:
focused- whether this widget should take focus or release it
-
setFormValue
Set the value property on the input element that backs this widget. This is the value that will be associated with the CheckBox's name and submitted to the server if aFormPanelthat holds it is submitted and the box is checked.Don't confuse this with
setValue(java.lang.Boolean), which actually checks and unchecks the box.- Parameters:
value-
-
setHTML
Description copied from interface:HasHTMLSets this object's contents via HTML. Use care when setting an object's HTML; it is an easy way to expose script-based security problems. Consider usingHasText.setText(String)whenever possible.- Specified by:
setHTMLin interfaceHasHTML- Overrides:
setHTMLin classButtonBase- Parameters:
html- the object's new HTML
-
setName
Description copied from interface:HasNameSets the widget's name. -
setTabIndex
public void setTabIndex(int index) Description copied from interface:FocusableSets 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-1will cause this widget to be removed from the tab order.- Specified by:
setTabIndexin interfaceFocusable- Overrides:
setTabIndexin classFocusWidget- Parameters:
index- the widget's tab index
-
setText
Description copied from interface:HasTextSets this object's text.- Specified by:
setTextin interfaceHasText- Overrides:
setTextin classButtonBase- Parameters:
text- the object's new text
-
setText
Description copied from interface:HasDirectionalTextSets this object's text, also declaring its direction.- Specified by:
setTextin interfaceHasDirectionalText- Parameters:
text- the object's new textdir- the text's direction
-
setValue
Checks or unchecks the check box.Note that this does not set the value property of the checkbox input element wrapped by this widget. For access to that property, see
setFormValue(String) -
setValue
Checks or unchecks the check box, firingValueChangeEventif appropriate.Note that this does not set the value property of the checkbox input element wrapped by this widget. For access to that property, see
setFormValue(String)- Specified by:
setValuein interfaceHasValue<Boolean>- Parameters:
value- true to check, false to uncheck; null value implies falsefireEvents- If true, and value has changed, fire aValueChangeEvent
-
setWordWrap
public void setWordWrap(boolean wrap) Description copied from interface:HasWordWrapSets whether word-wrapping is enabled.- Specified by:
setWordWrapin interfaceHasWordWrap- Parameters:
wrap-trueto enable word-wrapping.
-
sinkEvents
public void sinkEvents(int eventBitsToAdd) Description copied from class:WidgetOverridden to defer the call to super.sinkEvents until the first time this widget is attached to the dom, as a performance enhancement. Subclasses wishing to customize sinkEvents can preserve this deferred sink behavior by putting their implementation behind a check ofisOrWasAttached():@Override public void sinkEvents(int eventBitsToAdd) { if (isOrWasAttached()) { /* customized sink code goes here */ } else { super.sinkEvents(eventBitsToAdd); } }- Overrides:
sinkEventsin classWidget- Parameters:
eventBitsToAdd- a bitfield representing the set of events to be added to this element's event set- See Also:
-
ensureDomEventHandlers
protected void ensureDomEventHandlers() -
onEnsureDebugId
Affected Elements:- -label = label next to checkbox.
- Overrides:
onEnsureDebugIdin classUIObject- Parameters:
baseID- the base ID used by the main element- See Also:
-
onLoad
protected void onLoad()This method is called when a widget is attached to the browser's document. onAttach needs special handling for the CheckBox case. Must still callWidget.onAttach()to preserve theonAttachcontract. -
onUnload
protected void onUnload()This method is called when a widget is detached from the browser's document. Overridden because of IE bug that throws away checked state and in order to clear the event listener off of theinputElem. -
replaceInputElement
Replace the current input element with a new one. Preserves all state except for the name property, for nasty reasons related to radio button grouping. (See implementation ofRadioButton.setName(java.lang.String).)- Parameters:
elem- the new input element
-
replaceInputElement
Deprecated.Call and usereplaceInputElement(com.google.gwt.dom.client.Element)instead.
getValue()instead