Custom Action Types
You can create your own action types or override existing standard types in your project. These custom actions are then reusable across all application views, as illustrated in the examples below.
BaseAction
You can create your own action class and then assign it to a component. Below is a minimal example that makes the button show “Hello” when clicked.
-
Create an action class extending
BaseActionand add the@ActionTypeannotation with the desired type name:package com.company.onboarding.view.actions; import com.vaadin.flow.component.Component; import io.jmix.flowui.Notifications; import io.jmix.flowui.action.ActionType; import io.jmix.flowui.kit.action.BaseAction; import org.springframework.beans.factory.annotation.Autowired; @ActionType("myAction") public class MyCustomAction extends BaseAction { @Autowired private Notifications notifications; public MyCustomAction(String id) { super(id); setText("Click me!"); } @Override public void actionPerform (Component component) { notifications.create("Hello!") .withType(Notifications.Type.SUCCESS) .show(); } }If you want to override an existing action, register your new action class with the same type name in @ActionTypeannotation. -
Now you can use the action in view descriptors by specifying its type:
<action id="myAction" type="myAction"/> -
You can then assign it to a component:
<button id="actionBtn" action="myAction"/>
ItemsTrackingAction
If you need an action, which becomes enabled when one or more table rows are selected, use ItemTrackingAction. For example, imagine that you need an action that would show the instance name of the currently selected entity in a dataGrid. Below are the steps to create the action.
-
Create an action class and add the
@ActionTypeannotation with the desired type name:package com.company.onboarding.view.actions; import com.vaadin.flow.component.Component; import io.jmix.core.MetadataTools; import io.jmix.flowui.Notifications; import io.jmix.flowui.action.ActionType; import io.jmix.flowui.action.list.ItemTrackingAction; import org.springframework.beans.factory.annotation.Autowired; @ActionType("showSelected") public class ShowSelectedAction<E> extends ItemTrackingAction<E> { @Autowired private MetadataTools metadataTools; @Autowired private Notifications notifications; public ShowSelectedAction(String id) { super(id); setText("Show Selected"); } @Override public void actionPerform(Component component) { if (getTarget() != null) { E selected = getTarget().getSingleSelectedItem(); if (selected != null) { notifications.create(metadataTools.getInstanceName(selected)).show(); } } } }If you want to override an existing action, register your new action class with the same type name in @ActionTypeannotation. -
Now you can use the action in view descriptors by specifying its type:
<action id="showSelected" type="showSelected"/>