Checkbox - Listeners do not work (not even in the Sampler!)

I am trying to base the visibility of a few form fields on the state of a checkbox, so I need to know when the checkbox has been clicked to be able to update my form.

First I made my Form implement Button.ClickListener and implemented the method buttonClick. This is similar to the
checkbox example
in the Vaadin Sampler. The example in the sampler does not work, nothing happens when I activate the checkboxes.

@Override
public void buttonClick(ClickEvent event) {
	Button source = event.getButton();
	if (source == checkbox1) {
		showOptions((Boolean)checkbox1.getValue());
	}
}

Then I read in the Book of Vaadin that CheckBoxes should also work with ValueChangeListeners, so I implemented the example from the book:

checkbox1.addListener(new ValueChangeListener() {
	public void valueChange(ValueChangeEvent event) {
		showOptions((Boolean)checkbox1.getValue());
	}
});

Still no effect. What am I doing wrong? And why is the example in the Vaadin Sampler also broken?

Hi,

Is your checkbox set to immediate mode? If not, the state is transmitted to server only when some other component triggers a server roundtrip.

That was it! Thanks a lot!

I had checked what kind of settings the checkbox offers, but I must have missed the immediate part. At least I’m not alone with that kind of mistake, since someone did the same thing in the
sampler
:smiley:

The Sampler example has indeed been broken during refactoring, I made a ticket for this: http://dev.vaadin.com/ticket/3933

Thanks for noticing!

Best Regards,
Marc

Thanks a lot Henri! I had the same problem!

Hi,
I have more or less the same issue as Alex.
I have a checkbox for which I have added a ValueChangeListener.
As proposed Heuri, I have set to immediate my checkbox (but with or without, I have the same behaviour).

When I check it (checkbox value is true), it’s working fine, I catch the event.
When I uncheck it (checkbox value is false) it’s not working, nothing append, I don’t catch the event !!!

So it’s just working when I click the checkbox (value true) but not when I click again to uncheck it (value false).

The action of unchecking a checkbox should also raise a ValueChangeEvent, no ?

Best regards,