Reputation: 7
I am new in Vaadin and I have a problem.
I have tried with this: https://vaadin.com/web/magi/home/-/blogs/model-view-presenter-pattern-with-vaadin and example app is ok, but when I try to create something similar it's not working for me.
I have a simple login form (login, password fields, and one button). And there is a problem with the button. It's not triggered. I can click but the action is not started.
Here is my code:
public interface LoginView {
public void getNotification(String text);
interface LoginViewListener{
void loginButtonClick(String login, String password);
}
public void addListener(LoginViewListener listener);
}
public class LoginViewImpl extends CustomComponent implements LoginView,Button.ClickListener {
private TextField loginField = new TextField();
private PasswordField passwordField = new PasswordField();
private Label loginLabel = new Label("Login:");
private Label passwordLabel = new Label("Password:");
private Button loginButton = new Button("Login");
LoginViewListener listener;
public LoginViewImpl(){
VerticalLayout layout = new VerticalLayout();
layout.setSizeFull();
layout.addComponent(loginLabel);
layout.addComponent(loginField);
layout.addComponent(passwordLabel);
layout.addComponent(passwordField);
layout.addComponent(loginButton);
setCompositionRoot(layout);
}
public void getNotification(String text){
Notification.show(text);
}
@Override
public void buttonClick(Button.ClickEvent event) {
listener.loginButtonClick("jeden","dwa");
}
public void addListener(LoginViewListener listener) {
this.listener = listener;
}
}
public class LoginModel {
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String login;
private String password;
}
public class LoginPresenter implements LoginView.LoginViewListener {
LoginModel model;
LoginView view;
public LoginPresenter(LoginModel model, LoginView view){
this.model = model;
this.view = view;
view.addListener(this);
}
@Override
public void loginButtonClick(String login, String password) {
view.getNotification(login + " " + password);
}
}
@SpringUI(path = "")
public class MainClass extends UI {
@Override
protected void init(VaadinRequest request) {
LoginModel model = new LoginModel();
LoginViewImpl view = new LoginViewImpl();
new LoginPresenter(model, view);
VerticalLayout layout = new VerticalLayout();
layout.setSizeFull();
layout.addComponent(view);
setContent(layout);
}
}
Can you tell me what's wrong? :(
Upvotes: 0
Views: 388
Reputation: 13446
The problem is that LoginViewImpl
doesn't listen to the loginButton
events.
Check out how buttons are created in the MVP tutorial:
// Add buttons and have them send click events
// to this class
for (String caption: operations)
layout.addComponent(new Button(caption, this));
Notice the usage of a Button constructor overload which accepts ClickListener
. This way view implementation is registered as a handler of buttons' click events.
You should do something similar in your code:
public LoginViewImpl(){
//skip
loginButton.addClickListener(this);
layout.addComponent(loginButton);
setCompositionRoot(layout);
}
Upvotes: 1