我正致力于开发Vaadin应用程序。在开发Java桌面应用程序时,我使用AbstractAction来创建GUI的按钮。如何使用Vaadin
以下是我在Java Desktop应用程序中的操作:
// 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
}
}
编辑:
我已阅读this。这不是我要求的方式。
答案 0 :(得分:0)
从你的例子来看,它并不是很清楚你的要求是什么。 听起来你坚持使用反射,但我不确定为什么。你可以这样做,这与Button文档已经说过的基本相同。
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));
如果您需要的不仅仅是简单的方法调用,您还可以创建一个更精细的ClickListener实现,该实现具有多个buttonClick()方法。
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);
}
}