How can I create button's invoke method dynamically

i konw how do i create a button dynamically
but the button need a invoked method , and Each button has a different method
demand:
i need creat some Components through a loop,
when these Components be clicked , it will do something

So I thought about the button component, but i don’t know how to creat button’s invoke method dynamically,
the invoke method is like this :

 public void onButton_1Click(Component source) {
}

the button creat like this:
Button btn1=componentsFactory.createComponent(Button.class);

but the btn1 don’t have like this method: btn1 .setInvoke()

1 Like

Use the setAction() method instead.

For example:

Button button = componentsFactory.createComponent(Button.class);
button.setAction(new BaseAction("sayHello") {
    @Override
    public void actionPerform(Component component) {
        showNotification("Hello!");
    }
});

Or:

<actions>
    <action id="sayBye" invoke="sayBye"/>
</actions>
@Inject
private Action sayBye;

@Override
public void init(Map<String, Object> params) {
    Button button = componentsFactory.createComponent(Button.class);
    button.setAction(sayBye);
}

public void sayBye() {
    showNotification("Bye!");
}