I’m using Vaadin (version 7.6.3) with Spring boot (version 1.4.0).
I have a page with a TabSheet:
[code]
@SpringUI
public class VaadinUI extends UI {
@Autowired private tabA tabA;
@Autowired private tabB tabB;
@Override
protected void init(VaadinRequest vaadinRequest) {
TabSheet tabsheet = new TabSheet();
tabsheet.addTab(tabA, “Tab A”);
tabsheet.addTab(tabB, “Tab B”);
setContent(tabsheet);
}
}
[/code]It contains of two tabs.
Tab A:
[code]
@SpringComponent
@UIScope
public class tabA extends VerticalLayout {
@Autowired subWindow opA;
@PostConstruct
private void init() {
opA.setChangeHandler(e -> doSomethingWithChange( e));
Button optionABut = new Button("OptionA");
optionABut.addClickListener(e ->{
opA.optionA();
UI.getCurrent().addWindow(opA);
});
this.addComponent(optionABut);
}
private Object doSomethingWithChange(Object e) {
// TODO Auto-generated method stub
return null;
}
}
[/code]And tab B:
[code]
@SpringComponent
@UIScope
public class tabB extends VerticalLayout {
@Autowired subWindow opB;
@PostConstruct
private void init() {
opB.setChangeHandler(e -> doSomethingWithChange( e));
Button optionBBut = new Button("OptionB");
optionBBut.addClickListener(e ->{
opB.optionA();
UI.getCurrent().addWindow(opB);
});
this.addComponent(optionBBut);
}
private Object doSomethingWithChange(Object e) {
// TODO Auto-generated method stub
return null;
}
}
[/code]Both tabs opens the same component, a sub Window:
[code]
@SpringComponent
@UIScope
public class subWindow extends Window {
Button okButton = new Button("OK");
Label label = new Label();
subWindow(){
okButton.addClickListener(e -> {System.out.println("OK is clicked, from constructor without close");});
okButton.addClickListener(e -> {System.out.println("OK is clicked, from constructor with close");close();});
setContent(new VerticalLayout(label, okButton));
}
public void optionA(){
label.setCaption("Option A chosen");
}
public void optionB(){
label.setCaption("Option B chosen");
}
public interface ChangeHandler {
void onChange(Object var);
}
public void setChangeHandler(ChangeHandler h) {
okButton.addClickListener(e -> {System.out.println("OK is clicked, from changeHandler without close");});
okButton.addClickListener(e -> {System.out.println("OK is clicked, from changeHandler with close");close();});
}
}
[/code]If I run the application, clicks on one of the buttons to open the subwindow and then click the “okButton”, then I get the following result:
OK is clicked, from constructor without close
OK is clicked, from constructor with close
OK is clicked, from changeHandler without close
OK is clicked, from changeHandler with close
OK is clicked, from changeHandler with close
As seen, the clickListener in the “setChangeHandler”-method which includes “close();” is called 2 times. The others are only called 1 time. And I can’t figure out why there is this difference. Ideally I would like each listener to be activated only once when I click the button.
If I comment out one of the line “opB.setChangeHandler(e → doSomethingWithChange( e));” in tabB, then I get the following result, no matter whether which tab I’m opening the subwindow from:
OK is clicked, from constructor without close
OK is clicked, from constructor with close
OK is clicked, from changeHandler without close
OK is clicked, from changeHandler with close
Any suggestions as to what is happening? Why is it only the clicklistener which includes “close();” in the changeHandler that is called two times?