Class Checkbox

All Implemented Interfaces:
AttachNotifier, BlurNotifier<Checkbox>, ClickNotifier<Checkbox>, DetachNotifier, Focusable<Checkbox>, FocusNotifier<Checkbox>, HasAriaLabel, HasElement, HasEnabled, HasHelper, HasLabel, HasSize, HasStyle, HasValidation, HasValue<AbstractField.ComponentValueChangeEvent<Checkbox,Boolean>,Boolean>, HasValueAndElement<AbstractField.ComponentValueChangeEvent<Checkbox,Boolean>,Boolean>, HasClientValidation, HasTooltip, HasValidationProperties, InputField<AbstractField.ComponentValueChangeEvent<Checkbox,Boolean>,Boolean>, HasValidator<Boolean>, Serializable

@Tag("vaadin-checkbox") @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="24.4.10") @NpmPackage(value="@vaadin/checkbox",version="24.4.10") @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/checkbox/src/vaadin-checkbox.js") public class Checkbox extends AbstractSinglePropertyField<Checkbox,Boolean> implements ClickNotifier<Checkbox>, Focusable<Checkbox>, HasAriaLabel, HasClientValidation, HasValidationProperties, HasValidator<Boolean>, InputField<AbstractField.ComponentValueChangeEvent<Checkbox,Boolean>,Boolean>
Checkbox is an input field representing a binary choice.

Checkbox also has an indeterminate mode, see isIndeterminate() for more info.

Use CheckboxGroup to group related items. Individual checkboxes should be used for options that are not related to each other in any way.

Author:
Vaadin Ltd
See Also:
  • Constructor Details

  • Method Details

    • onAttach

      protected void onAttach(AttachEvent attachEvent)
      Description copied from class: Component
      Called when the component is attached to a UI.

      This method is invoked before the AttachEvent is fired for the component.

      Make sure to call super.onAttach when overriding this method.
      Overrides:
      onAttach in class Component
      Parameters:
      attachEvent - the attach event
    • getLabel

      public String getLabel()
      Get the current label text.
      Specified by:
      getLabel in interface HasLabel
      Returns:
      the current label text
    • setLabel

      public void setLabel(String label)
      Set the current label text of this checkbox.
      Specified by:
      setLabel in interface HasLabel
      Parameters:
      label - the label text to set
    • setLabelComponent

      public void setLabelComponent(Component component)
      Replaces the label content with the given label component.
      Parameters:
      component - the component to be added to the label.
      Since:
      23.1
    • setAriaLabel

      public void setAriaLabel(String ariaLabel)
      Description copied from interface: HasAriaLabel
      Set the aria-label of the component to the given text.

      This method should not be used if HasAriaLabel.setAriaLabelledBy(String) is also used. If both attributes are present, aria-labelledby will take precedence over aria-label.

      Specified by:
      setAriaLabel in interface HasAriaLabel
      Parameters:
      ariaLabel - the aria-label text to set or null to clear
    • getAriaLabel

      public Optional<String> getAriaLabel()
      Description copied from interface: HasAriaLabel
      Gets the aria-label of the component.
      Specified by:
      getAriaLabel in interface HasAriaLabel
      Returns:
      an optional aria-label of the component if no aria-label has been set
    • setAriaLabelledBy

      public void setAriaLabelledBy(String ariaLabelledBy)
      Description copied from interface: HasAriaLabel
      Set the aria-labelledby of the component. The value must be a valid id attribute of another element that labels the component. The label element must be in the same DOM scope of the component, otherwise screen readers may fail to announce the label content properly.

      This method should not be used if HasAriaLabel.setAriaLabel(String) is also used. If both attributes are present, aria-labelledby will take precedence over aria-label.

      Specified by:
      setAriaLabelledBy in interface HasAriaLabel
      Parameters:
      ariaLabelledBy - the string with the id of the element that will be used as label or null to clear
    • getAriaLabelledBy

      public Optional<String> getAriaLabelledBy()
      Description copied from interface: HasAriaLabel
      Gets the aria-labelledby of the component
      Specified by:
      getAriaLabelledBy in interface HasAriaLabel
      Returns:
      an optional aria-labelledby of the component if no aria-labelledby has been set
    • setAutofocus

      public void setAutofocus(boolean autofocus)
      Set the checkbox to be input focused when the page loads.
      Parameters:
      autofocus - the boolean value to set
    • isAutofocus

      public boolean isAutofocus()
      Get the state for the auto-focus property of the checkbox.

      This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.

      Returns:
      the autofocus property from the checkbox
    • setIndeterminate

      public void setIndeterminate(boolean indeterminate)
      Set the indeterminate state of the checkbox.

      NOTE: As according to the HTML5 standard, this has only effect on the visual appearance, not on the checked value!

      Parameters:
      indeterminate - the boolean value to set
      See Also:
    • isIndeterminate

      @Synchronize(property="indeterminate", value="indeterminate-changed") public boolean isIndeterminate()
      Get the indeterminate state of the checkbox. The default value is false.

      An indeterminate checkbox is neither checked nor unchecked. A typical use case is a “Select All” checkbox indicating that some, but not all, items are selected. When the user clicks an indeterminate checkbox, it is no longer indeterminate, and the checked value also changes.

      NOTE: As according to the HTML5 standard, this has only effect on the visual appearance, not on the checked value!

      Returns:
      the indeterminate property from the checkbox
    • setManualValidation

      public void setManualValidation(boolean enabled)
      Description copied from interface: HasValidation
      Sets whether manual validation mode is enabled for the component.

      When enabled, the component doesn't perform its built-in constraint validation on value change, blur, and other events. This allows manually controlling the invalid state and error messages using the HasValidation.setInvalid(boolean) and HasValidation.setErrorMessage(String) methods. Manual mode is helpful when there is a need for a totally custom validation logic that cannot be achieved with Binder.

      Example:

       Field field = new Field();
       field.setManualValidation(true);
       field.addValueChangeListener(event -> {
           if (Objects.equal(event.getValue(), "")) {
               field.setInvalid(true);
               field.setErrorMessage("The field is required.");
           } else {
               field.setInvalid(false);
           }
       });
       

      For components that don't have built-in validation, the method has no effect.

      Specified by:
      setManualValidation in interface HasValidation
      Parameters:
      enabled - whether to enable manual validation mode.
    • validate

      protected void validate()
      Performs server-side validation of the current value. This is needed because it is possible to circumvent the client-side validation constraints using browser development tools.