Events on create UI

I have this sample:

Class MyComp{
   private final CheckBox checkbox;
 public Registration addMyEventListener(
            ComponentEventListener<MyEvent> listener) {
        return addListener(MyEvent.class, listener);
    }
 public MyComp(int value){
   checkbox.addValueChangeListener(evt-> fireEvent(new MyEvent(this, true, evt.getValue()));
}
}

Class view {
  private final MyComp myComp;
public View(){
  myComp.addMyEventListener(item -> System.out.println("teste");
}
}

So when my user click on my checkbox. the System.out.print are executed.

But now I need to change in my MyComp constructor to if the value > 10 for sample the checkbox need to start checked so I put one


 public MyComp(int value){
   checkbox.addValueChangeListener(evt-> fireEvent(new MyEvent(this, true, evt.getValue()));
  checkbox.setValue(value > 10)
}

But when I does this the System.out. dont run… how can I fix this? I already try to fire the event just like this:

if(value > 0)
fireEvent(new MyEvent(this, true, evt.getValue()))

but without success too…

can anyone help me?

tks

I assume the issue here is, that you add your MyEvent listener after the MyComp has been created. But since MyComp already sets the value and thus triggers the value change listener of the combobox, there is simply no MyEvent listener yet.

So to say the problem is an order problem

  1. MyComp created
  2. CB value change listener added
  3. CB value set (triggers VCEvent, that fires the MyEvent, but MyEvent has no listener yet)
  4. MyComp adds the MyEvent listener

So in your case you either need to set the CB value after you added the MyEvent listener via some MyComp api or you have to init the MyEvent listener inside the constructor and pass the callback via the constructor, too. But that would somehow make the listener itself irrelevant.