-2

I'm working on developing an Vaadin application. When developing a Java desktop application I use AbstractAction to create the buttons of my GUI. How to do that using Vaadin

Here is how I do in Java Desktop application:

// In my  view
JButton button = new JButton(new Action(BUTTON_NAME, presenter, "methodToInvoke", Object... arguments));

class Action extends AbstractAction {

    public Action(ButtonName name, Presenter presenter, String method, Object... arguments) {
        this.name = name;
        this.presenter = presenter;
        this.method = method;
        this.arguments = arguments;
        readButtonProperties();
    }

    public void actionPerformed(ActionEvent e) {
         //Call method from presenter with arguments using reflection
    }
}

Edit:

I already read this. It's not the way I'm asking for.

Hunsu
  • 3,281
  • 7
  • 29
  • 64

1 Answers1

0

From your example it isn't really clear what your requirements are. Sounds like you insist on using reflection, but I'm not sure why. You could just do this, which is basically the same thing the Button documentation already says.

    Presenter presenter = getPresenter();
    String with = "with";
    int my = 42;
    List<Data> = new List<>();
    Button vaadinButton = new Button("I'm a button.", clickEvent -> presenter.doStuff(with, my, arguments));

If you need more than a simple method call, you could just as well create a more elaborate ClickListener implementation that has more than the buttonClick() method.

Button vaadinButton = new Button("Button Caption", new Action(...))


class Action extends AbstractAction implements Button.ClickListener {
    public Action(ButtonName name, Presenter presenter, String method, Object... arguments) {
        this.name = name;
        this.presenter = presenter;
        this.method = method;
        this.arguments = arguments;
        readButtonProperties();
    }

    public void actionPerformed(ActionEvent e) {
         //Call method from presenter with arguments using reflection
    }

    @Override
    public void buttonClick(ClickEvent event) {
        actionPerformed(null);
    }
}
Flo
  • 2,738
  • 1
  • 16
  • 12
  • The problem with this is that all the button initaialisation is done in View and I want to avoid that. Look at this answer http://stackoverflow.com/questions/11438048/best-practice-for-implementing-actionlistener – Hunsu Feb 05 '15 at 10:48