button
A button performs an action when a user clicks on it.
-
XML element:
button
-
Java class:
JmixButton
Basics
A button can have a text, an icon, or both:
An example of defining a button with text, icon and a tooltip (title
attribute) retrieved from the message bundle:
<button id="toolsButton"
text="Tools"
title="msg://toolsButton.title"
icon="TOOLS"/>
To handle button clicks, subscribe to the ClickEvent
in the view class:
@Subscribe("toolsButton")
public void onToolsButtonClick(ClickEvent<Button> event) {
// ...
}
Action
Another way of handling button clicks is associating the button with an Action:
<actions>
<action id="helloAction" text="Say Hello"/>
</actions>
<layout>
<button id="helloButton" action="helloAction"/>
You can associate a button with any action defined in the view or in a component implementing the HasActions
interface, for example DataGrid
. In the following example, a button is associated with the create
action of the usersTable
component:
<button id="createButton" action="usersTable.create"/>
<dataGrid id="usersTable" dataContainer="users">
<actions>
<action id="create" type="create"/>
</actions>
Attributes
autofocus - action - classNames - disableOnClick - enabled - height - icon - iconAfterText - id - maxHeight - maxWidth - minHeight - minWidth - text - themeNames - title visible - whiteSpace - width
disableOnClick
If the disableOnClick
attribute is set to true
, the button will be automatically disabled when clicked, typically to prevent (accidental) extra clicks on a button. You can later re-enable the button by invoking the setEnabled(true)
method.
icon
Allows to set the Icon
component for the button. There are two built-in icon sets:
-
Lumo Icons. You can set the Lumo icon in the view class:
@ViewComponent protected JmixButton lumoBtn; @Subscribe public void onInit(InitEvent event) { Icon lumoIcon = new Icon("lumo", "photo"); lumoBtn.setIcon(lumoIcon); }
-
Vaadin Icons. The icons from this set have constants. You can set the Vaadin icon in the view class:
@ViewComponent protected JmixButton vaadinBtn; @Subscribe public void onInit(InitEvent event) { Icon vaadinIcon = new Icon(VaadinIcon.PHONE); vaadinBtn.setIcon(vaadinIcon); }
or in the XML descriptor:
<button id="vaadinBtn" icon="ALARM"/>
iconAfterText
If the iconAfterText
attribute is set to true
, this button’s icon should be positioned after it’s text content.
Handlers
To generate a handler stub in Jmix Studio, use the Handlers tab of the Jmix UI inspector panel or the Generate Handler action available in the top panel of the view class and through the Code → Generate menu (Alt+Insert / Cmd+N). |
ClickEvent
com.vaadin.flow.component.ClickEvent
is sent when the user clicks on the button.
@Subscribe("helloButton") (1)
public void onHelloButtonClick(ClickEvent<Button> event) {
Button button = event.getSource(); (2)
// ...
}
1 | The @Subscribe annotation connects the handler method to the component by the component’s id. |
2 | If needed, you can get the clicked button from the event object. |
To register the event handler programmatically, use the addClickListener()
component method.
See Also
See Vaadin Docs for more information.