Class ActionButton<T>
- Type Parameters:
T- the type of the result of the slow task
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasStyle,Serializable
NOTE! This class is still in early development and likely to get some changes still in the future. Suggestions/contributions are more than welcome!
The button is disabled while the task is running (which blocks re-triggering) and re-enabled when
it is done; aria-busy marks it busy for assistive technology. The busy state is shown as
a built-in (indeterminate) progress bar for buttons with text, and — since a greyed-out static
icon is poor feedback and there is no room for a bar — as a spinner replacing the icon for
icon-only buttons. A trackable/estimated action shows the (determinate) progress bar even on an
icon-only button. See isShowSpinner() / setShowSpinner(boolean) /
setBusyIcon(Component) and isShowProgressBar() / setShowProgressBar(boolean).
The actual task, set with setAction(Supplier) or setCompletableFutureAction(Supplier), is run in a
separate thread. If your task wants to update the UI during its execution, you need to synchronize with the UI thread
using UI.access(Command).
UI updates are suggested to be done in #setPostTaskAction(Consumer) which is called after the task has completed or #setPreTaskAction(Runnable) which is called before the task is started. These are run in the UI thread.
The button will automatically enable polling if push is not enabled and call push() if "manual server push" is active.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionActionButton(String buttonText) ActionButton(String buttonText, Runnable action) ActionButton(String buttonText, Supplier<CompletableFuture<T>> action) -
Method Summary
Modifier and TypeMethodDescriptionaddThemeVariants(com.vaadin.flow.component.button.ButtonVariant... variants) Adds theme variants to the underlyingVButton.protected com.vaadin.flow.component.ComponentCreates the default busy indicator: an emptyvaadin-icondrawn as a spinning ring purely in CSS (seeaction-button.css).Returns the underlying button component, for further configaration.booleanbooleanbooleanWhether, while the task runs, the button's icon is replaced by a spinning busy indicator (the button is also disabled, as always).protected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) protected VProgressBarprotected voidremoveThemeVariants(com.vaadin.flow.component.button.ButtonVariant... variants) Removes theme variants from the underlyingVButton.setAriaLabel(String ariaLabel) Sets the accessible name of the underlyingVButton.setBusyIcon(com.vaadin.flow.component.Component busyIcon) Sets a custom component to show in place of the icon while the task runs (instead of the default spinner).setBusyText(String text) Set the text of the button to show while the task is running (and button disabled).setEnableAfterAction(boolean enableAfterAction) voidsetEstimatedDuration(Integer estimatedDuration) Set the estimated duration of the task in milliseconds.voidsetExecutor(Executor executor) Set an executor to be used for running the task.setIcon(com.vaadin.flow.component.Component icon) Sets the button's icon.setPostUiAction(Consumer<? super T> postUiAction) Sets an action to update the UI after the actual slow actions is completed.setPostUiUpdate(Consumer<? super T> postUiUpdate) Deprecated.setPreUiAction(Runnable preUiAction) Sets an action to update the UI before the actual slow actions is started.setPreUiUpdate(Runnable preUiUpdate) Deprecated, for removal: This API element is subject to removal in a future version.usesetPreUiAction(Runnable)insteadvoidsetShowProgressBar(boolean showProgressBar) setShowSpinner(boolean showSpinner) Forces the spinning busy indicator on or off (seeisShowSpinner()for the default behavior).voidsetTooltipText(String tooltipText) Sets the tooltip of the underlyingVButton.voidupdateProgressAsync(double progress) API for updating the progress bar from the task.voidupdateProgressAsync(double progress, double min, double max) API for updating the progress bar from the task.Methods inherited from class com.vaadin.flow.component.Composite
getChildren, getContent, getElement, initContentMethods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
ActionButton
public ActionButton() -
ActionButton
-
ActionButton
-
ActionButton
-
-
Method Details
-
setAction
-
setAction
-
setCompletableFutureAction
-
setPostUiAction
Sets an action to update the UI after the actual slow actions is completed. This is "run in the UI thread", meaning you don't need to synchronize with the UI using UI.access().- Parameters:
postUiAction- the action to run after the slow task- Returns:
- this for chaining
-
setPostUiUpdate
Deprecated.usesetPostUiAction(Consumer)instead- Parameters:
postUiUpdate- the action to run after the slow task- Returns:
- this for chaining
-
setPreUiAction
Sets an action to update the UI before the actual slow actions is started. This is "run in the UI thread", meaning you don't need to synchronize with the UI using UI.access().- Parameters:
preUiAction- the action to run before the slow task- Returns:
- this for chaining
-
setPreUiUpdate
Deprecated, for removal: This API element is subject to removal in a future version.usesetPreUiAction(Runnable)instead -
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin classcom.vaadin.flow.component.Component
-
getCompletableFuture
-
setText
-
setIcon
Sets the button's icon. Delegates to the underlyingVButton. -
addThemeVariants
Adds theme variants to the underlyingVButton. -
removeThemeVariants
public ActionButton<T> removeThemeVariants(com.vaadin.flow.component.button.ButtonVariant... variants) Removes theme variants from the underlyingVButton. -
setAriaLabel
Sets the accessible name of the underlyingVButton. -
setTooltipText
Sets the tooltip of the underlyingVButton. -
setBusyText
Set the text of the button to show while the task is running (and button disabled).- Parameters:
text- the text to show- Returns:
-
reEnableAfterAction
protected void reEnableAfterAction() -
createSpinner
protected com.vaadin.flow.component.Component createSpinner()Creates the default busy indicator: an emptyvaadin-icondrawn as a spinning ring purely in CSS (seeaction-button.css). Being avaadin-icon, the theme sizes it exactly like the icon it replaces (icon sizes vary a lot between themes), while the ring's thickness is under our control rather than baked into a glyph. OverridesetBusyIcon(Component)for a custom one. -
isShowSpinner
public boolean isShowSpinner()Whether, while the task runs, the button's icon is replaced by a spinning busy indicator (the button is also disabled, as always). By default this is enabled automatically for icon-only buttons (an icon, no text), where a greyed-out static icon is poor feedback and there is no room for a progress bar. Buttons with text (or that otherwise show aprogress bar) use that bar instead. Set it explicitly withsetShowSpinner(boolean)to force it on or off. -
setShowSpinner
Forces the spinning busy indicator on or off (seeisShowSpinner()for the default behavior). Call before the button is attached. -
setBusyIcon
Sets a custom component to show in place of the icon while the task runs (instead of the default spinner). ImpliesisShowSpinner(). -
isShowProgressBar
public boolean isShowProgressBar() -
setShowProgressBar
public void setShowProgressBar(boolean showProgressBar) - Parameters:
showProgressBar- true if the built-in progress bar should be shown while the task is running
-
prepareProgressBar
-
updateProgressAsync
public void updateProgressAsync(double progress, double min, double max) API for updating the progress bar from the task. This method is safe to call from the task thread.- Parameters:
progress- the progress value to setmin- the minimum value of the progress barmax- the maximum value of the progress bar
-
updateProgressAsync
public void updateProgressAsync(double progress) API for updating the progress bar from the task. This method is safe to call from the task thread.- Parameters:
progress- the progress value to set
-
getExecutor
-
setExecutor
Set an executor to be used for running the task. If not set, the default executor is used.- Parameters:
executor- the executor to use for the task
-
getEstimatedDuration
-
setEstimatedDuration
Set the estimated duration of the task in milliseconds. This can be used to show a progress bar that is not indeterminate.- Parameters:
estimatedDuration-
-
getButton
Returns the underlying button component, for further configaration.- Returns:
- the underlying button component
-
isEnableAfterAction
public boolean isEnableAfterAction()- Returns:
- true if the button should be automatically enabled after the action is completed
-
setEnableAfterAction
- Parameters:
enableAfterAction- true (default) if the button should be automatically enabled after the action is completed- Returns:
- this for chaining
-
setPostUiAction(Consumer)instead