Class Button

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.button.Button
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.BlurNotifier<Button>, com.vaadin.flow.component.ClickNotifier<Button>, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.Focusable<Button>, com.vaadin.flow.component.FocusNotifier<Button>, com.vaadin.flow.component.HasAriaLabel, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasText, com.vaadin.flow.component.HasTheme, com.vaadin.flow.component.shared.HasPrefix, com.vaadin.flow.component.shared.HasSuffix, com.vaadin.flow.component.shared.HasThemeVariant<ButtonVariant>, com.vaadin.flow.component.shared.HasTooltip, Serializable

@Tag("vaadin-button") @NpmPackage(value="@vaadin/button", version="25.0.0-alpha21") @JsModule("@vaadin/button/src/vaadin-button.js") public class Button extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.ClickNotifier<Button>, com.vaadin.flow.component.Focusable<Button>, com.vaadin.flow.component.HasAriaLabel, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.shared.HasPrefix, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.shared.HasSuffix, com.vaadin.flow.component.HasText, com.vaadin.flow.component.shared.HasThemeVariant<ButtonVariant>, com.vaadin.flow.component.shared.HasTooltip
The Button component allows users to perform actions. It comes in several different style variants, and supports icons in addition to text labels.
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier

    com.vaadin.flow.component.BlurNotifier.BlurEvent<C extends com.vaadin.flow.component.Component>

    Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier

    com.vaadin.flow.component.FocusNotifier.FocusEvent<C extends com.vaadin.flow.component.Component>

    Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasText

    com.vaadin.flow.component.HasText.WhiteSpace
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Button(com.vaadin.flow.component.Component icon)
    Creates a button with an icon inside.
    Button(com.vaadin.flow.component.Component icon, com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.ClickEvent<Button>> clickListener)
    Creates a button with an icon and a listener for click events.
    Button(String text)
    Creates a button with a text inside.
    Button(String text, com.vaadin.flow.component.Component icon)
    Creates a button with a text and an icon inside.
    Button(String text, com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.ClickEvent<Button>> clickListener)
    Creates a button with a text and a listener for click events.
    Button(String text, com.vaadin.flow.component.Component icon, com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.ClickEvent<Button>> clickListener)
    Create a button with a text, an icon and a listener for click events.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.vaadin.flow.shared.Registration
    addBlurListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.BlurNotifier.BlurEvent<Button>> listener)
    com.vaadin.flow.shared.Registration
    addFocusListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.FocusNotifier.FocusEvent<Button>> listener)
    com.vaadin.flow.component.ShortcutRegistration
    addFocusShortcut(com.vaadin.flow.component.Key key, com.vaadin.flow.component.KeyModifier... keyModifiers)
    void
    Simulates a click on this button on the server side if it is enabled.
    void
    Executes a click on this button at the client-side.
    com.vaadin.flow.component.Component
    Gets the component that is defined as the icon of this button.
    boolean
    Get the state for the auto-focus property of the button.
    boolean
    Gets whether the button is set to be disabled when clicked.
    boolean
    Gets whether this button's icon is positioned after it's text content or the other way around.
    protected void
    remove(com.vaadin.flow.component.Component... components)
    Removes the given child components from this component.
    void
    setAutofocus(boolean autofocus)
    Set the button to be input focused when the page loads.
    void
    setDisableOnClick(boolean disableOnClick)
    Sets whether the button should be disabled when clicked.
    void
    setEnabled(boolean enabled)
    Sets the button explicitly disabled or enabled.
    void
    setIcon(com.vaadin.flow.component.Component icon)
    Sets the given component as the icon of this button.
    void
    setIconAfterText(boolean iconAfterText)
    Sets whether this button's icon should be positioned after it's text content or the other way around.
    void
    Sets the given string as the text content of this component.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.ClickNotifier

    addClickListener, addClickShortcut, addDoubleClickListener, addSingleClickListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.Focusable

    blur, focus, getTabIndex, setTabIndex

    Methods inherited from interface com.vaadin.flow.component.HasAriaLabel

    getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasEnabled

    isEnabled

    Methods inherited from interface com.vaadin.flow.component.shared.HasPrefix

    getPrefixComponent, setPrefixComponent

    Methods inherited from interface com.vaadin.flow.component.HasSize

    getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName

    Methods inherited from interface com.vaadin.flow.component.shared.HasSuffix

    getSuffixComponent, setSuffixComponent

    Methods inherited from interface com.vaadin.flow.component.HasText

    getText, getWhiteSpace, setWhiteSpace

    Methods inherited from interface com.vaadin.flow.component.HasTheme

    addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName

    Methods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant

    addThemeVariants, removeThemeVariants, setThemeVariant, setThemeVariants, setThemeVariants

    Methods inherited from interface com.vaadin.flow.component.shared.HasTooltip

    getTooltip, setTooltipMarkdown, setTooltipText
  • Constructor Details

    • Button

      public Button()
      Default constructor. Creates an empty button.
    • Button

      public Button(String text)
      Creates a button with a text inside.
      Parameters:
      text - the text inside the button
      See Also:
    • Button

      public Button(com.vaadin.flow.component.Component icon)
      Creates a button with an icon inside.
      Parameters:
      icon - the icon inside the button
      See Also:
    • Button

      public Button(String text, com.vaadin.flow.component.Component icon)
      Creates a button with a text and an icon inside.

      Use setIconAfterText(boolean) to change the order of the text and the icon.

      Parameters:
      text - the text inside the button
      icon - the icon inside the button
      See Also:
    • Button

      public Button(String text, com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.ClickEvent<Button>> clickListener)
      Creates a button with a text and a listener for click events.
      Parameters:
      text - the text inside the button
      clickListener - the event listener for click events
      See Also:
    • Button

      public Button(com.vaadin.flow.component.Component icon, com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.ClickEvent<Button>> clickListener)
      Creates a button with an icon and a listener for click events.
      Parameters:
      icon - the icon inside the button
      clickListener - the event listener for click events
      See Also:
    • Button

      public Button(String text, com.vaadin.flow.component.Component icon, com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.ClickEvent<Button>> clickListener)
      Create a button with a text, an icon and a listener for click events.
      Parameters:
      text - the text inside the button
      icon - the icon inside the button
      clickListener - the event listener for click events
      See Also:
  • Method Details

    • setText

      public void setText(String text)
      Sets the given string as the text content of this component.

      This method removes any existing text-content and replaces it with the given text.

      This method also sets or removes this button's theme=icon attribute based on whether this button contains only an icon after this operation or not.

      Specified by:
      setText in interface com.vaadin.flow.component.HasText
      Parameters:
      text - the text content to set, may be null to only remove existing text
    • setIcon

      public void setIcon(com.vaadin.flow.component.Component icon)
      Sets the given component as the icon of this button.

      Even though you can use almost any component as an icon, some good options are Icon and Image.

      Use setIconAfterText(boolean) to change the icon's position relative to the button's text content.

      This method also sets or removes this button's theme=icon attribute based on whether this button contains only an icon after this operation or not.

      Parameters:
      icon - component to be used as an icon, may be null to only remove the current icon, can't be a text-node
    • getIcon

      public com.vaadin.flow.component.Component getIcon()
      Gets the component that is defined as the icon of this button.
      Returns:
      the icon of this button, or null if the icon is not set
    • setIconAfterText

      public void setIconAfterText(boolean iconAfterText)
      Sets whether this button's icon should be positioned after it's text content or the other way around.

      At the element-level, this method determines whether to set slot="prefix" or slot="suffix" attribute to the icon.

      Parameters:
      iconAfterText - whether the icon should be positioned after the text content or not
    • isIconAfterText

      public boolean isIconAfterText()
      Gets whether this button's icon is positioned after it's text content or the other way around.
      Returns:
      true if this button positions it's icon after it's text content, false otherwise
    • click

      public void click()
      Simulates a click on this button on the server side if it is enabled. Calling this method executes all registered click listeners on the server side, but does not execute possible client side registered listeners.
      See Also:
    • clickInClient

      public void clickInClient()
      Executes a click on this button at the client-side. Calling this method behaves the same as if the user would have clicked on the button.
    • setAutofocus

      public void setAutofocus(boolean autofocus)
      Set the button 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 button.

      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 button
    • setDisableOnClick

      public void setDisableOnClick(boolean disableOnClick)
      Sets whether the button should be disabled when clicked.

      When set to true, the button will be immediately disabled on the client-side when clicked, preventing further clicks until re-enabled from the server-side.

      Parameters:
      disableOnClick - whether the button should be disabled when clicked
    • isDisableOnClick

      public boolean isDisableOnClick()
      Gets whether the button is set to be disabled when clicked.
      Returns:
      whether button is set to be disabled on click
    • setEnabled

      public void setEnabled(boolean enabled)
      Sets the button explicitly disabled or enabled. When disabled, the button is rendered as "dimmed" and prevents all user interactions (mouse and keyboard).

      Since disabled buttons are not focusable and cannot react to hover events by default, it can cause accessibility issues by making them entirely invisible to assistive technologies, and prevents the use of Tooltips to explain why the action is not available. This can be addressed with the feature flag accessibleDisabledButtons, which makes disabled buttons focusable and hoverable, while preventing them from being triggered. To enable this feature flag, add the following line to src/main/resources/vaadin-featureflags.properties:

       com.vaadin.experimental.accessibleDisabledButtons = true
       
      This feature flag will also enable focus events and focus shortcuts for disabled buttons.
      Specified by:
      setEnabled in interface com.vaadin.flow.component.HasEnabled
    • addFocusShortcut

      public com.vaadin.flow.component.ShortcutRegistration addFocusShortcut(com.vaadin.flow.component.Key key, com.vaadin.flow.component.KeyModifier... keyModifiers)

      By default, focus shortcuts are only active when the button is enabled. To make disabled buttons also focusable, enable the following feature flag in src/main/resources/vaadin-featureflags.properties:

       com.vaadin.experimental.accessibleDisabledButtons = true
       
      This feature flag will enable focus events and focus shortcuts for disabled buttons.
      Specified by:
      addFocusShortcut in interface com.vaadin.flow.component.Focusable<Button>
    • addFocusListener

      public com.vaadin.flow.shared.Registration addFocusListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.FocusNotifier.FocusEvent<Button>> listener)

      By default, buttons are only focusable in the enabled state. To make disabled buttons also focusable, enable the following feature flag in src/main/resources/vaadin-featureflags.properties:

       com.vaadin.experimental.accessibleDisabledButtons = true
       
      This feature flag will enable focus events and focus shortcuts for disabled buttons.
      Specified by:
      addFocusListener in interface com.vaadin.flow.component.FocusNotifier<Button>
    • addBlurListener

      public com.vaadin.flow.shared.Registration addBlurListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.BlurNotifier.BlurEvent<Button>> listener)

      By default, buttons are only focusable in the enabled state. To make disabled buttons also focusable, enable the following feature flag in src/main/resources/vaadin-featureflags.properties:

       com.vaadin.experimental.accessibleDisabledButtons = true
       
      Specified by:
      addBlurListener in interface com.vaadin.flow.component.BlurNotifier<Button>
    • remove

      protected void remove(com.vaadin.flow.component.Component... components)
      Removes the given child components from this component.
      Parameters:
      components - The components to remove.
      Throws:
      IllegalArgumentException - if any of the components is not a child of this component.