button
The button component allows users to trigger an action.
XML Element |
|
|---|---|
Java Class |
|
Basics
A button can display text, an icon, or both. Use the title attribute to provide a tooltip.
<button id="helloButton"
text="Say hello!"
title="Click me!"/>
<button id="saveButton1"
text="Save"
icon="ARCHIVE"/>
<button id="saveButton2"
title="Save"
icon="ARCHIVE"/>
Button with Declarative Action
Use the action attribute to associate a button with an existing action. The button automatically reflects the action’s text, icon, enabled and visible states, and invokes the action when clicked.
For a view action, set action to its identifier. For an action defined by another component, use the <componentId>.<actionId> format.
<actions>
<action id="someAction"
text="Click me!"/>
</actions>
<layout>
<button action="someAction"/>
</layout>
Button with Programmatic Action
You can create an action in Java and assign it to a button using the setAction() method. The action defines the button text and the operation performed when the button is clicked.
<button id="buttonAction"/>
@ViewComponent
protected JmixButton buttonAction;
@Autowired
protected Notifications notifications;
@Subscribe
protected void onInit(InitEvent event) {
buttonAction.setAction(
new BaseAction("action")
.withText("Click me!")
.withHandler(actionPerformedEvent -> notifications.show("Action performed"))
);
}
Button with Image
A button’s icon can be a component, such as an image. The following example loads an image and assigns it to the button using the setIcon() method.
<button id="logoButton" themeNames="icon"/>
private static final String LOGO_SRC_PATH = "/META-INF/resources/icons/jmix-logo.png";
@ViewComponent
private JmixButton logoButton;
@Subscribe
public void onInit(InitEvent event) {
Image logo = new Image(DownloadHandler.forClassResource(getClass(), LOGO_SRC_PATH), "jmix-logo");
logo.setWidth("100px");
logoButton.setIcon(logo);
}
Theme Variants
Use the themeNames attribute to apply one or more theme variants.
| Variant | Description | Supported By |
|---|---|---|
|
Applies the primary button style. |
Aura, Lumo |
|
Applies the success style. |
Aura, Lumo |
|
Applies the warning style. |
Aura, Lumo |
|
Applies the error style. |
Aura, Lumo |
|
Applies the contrast style. |
Lumo |
|
Makes the button larger. |
Aura, Lumo |
|
Makes the button smaller. |
Aura, Lumo |
|
Reduces the whitespace around the icon. |
Aura, Lumo |
|
Applies a lower-emphasis button style. |
Aura, Lumo |
|
Applies an inline button style intended for use inside text content. |
Lumo |
Attributes
The following attributes are specific to button:
| Name | Description | Default |
|---|---|---|
|
Associates the button with a view or component action. See Button with Declarative Action. |
— |
Disables the button immediately after it is clicked to prevent repeated submissions. Use |
|
|
Sets the icon displayed by the button. |
— |
|
Places the icon after the button text. |
|
|
Controls how whitespace and line breaks are handled in the button text. |
|
The following shared attributes are supported by button:
id - alignSelf - ariaLabel - ariaLabelledBy - autofocus - classNames - css - enabled - focusShortcut - height - maxHeight - maxWidth - minHeight - minWidth - tabIndex - text - themeNames - title - visible - width
Handlers
The following handlers are specific to button:
| Name | Description |
|---|---|
Fired when the user clicks the button. To register the handler programmatically, use the |
|
Fired when the user clicks the button twice within a short interval. |
|
Fired after a short delay when the click is determined not to be part of a double-click. |
The following shared handlers are supported by button:
Elements
A button can include icon, prefix and suffix, and tooltip components as its nested elements.