Why default value of an unchecked CheckBox is null?

According to
the docs
, “CheckBox is a two-state selection component that can be either checked or unchecked” { … } “Clicking on a check box will change its state. The state is a Boolean property that you can set with the setValue() method and obtain with the getValue() method of the Property interface”.

However, if you put a CheckBox without explicitly setting a value (that is, it will be displayed as an unchecked checkbox) with
setValue()
and retrieve its value with a
getValue()
, you realize that is value is
null
.

I understand that the Java
Boolean
class can be
null
, but what sounds unconsistent to me is that the possible value representation of the “visual representation” of an unchecked checkbox may be actually one of two possible values: false or null, thus effectively converting the CheckBox control in a
three-valuable

two-state
control.

Shouldn’t this be clearly stated in the manual? It took me some time to figure out what was happening. I think it should be noted that if no explicit value is set during CheckBox initialization,
null
is the default until it gets checked / unchecked…

This is prone to errors as actually an unchecked checkbox may mean eiter that the value is
false
or that we don’t know what the value is…

Also, unintentioned or not, the Vaadin sampler example shows a checkbox checked by default, so there’s no way to realize this behaviour from there… Shouldn’t be better to show an unchecked box by default, with a
Label
showing the current value (even without having clicked it for the 1st time), to clearly demonstrate the values it may hold?

Hi,

that sounds like a legit bug to me, do you care to
file a bug report
?

Regards,

Olli

Thanks a lot for your reply, Olli.

Sorry Olli but I just discovered what is the source of this behaviour: I had my CheckBox created via FieldGroup.buildAndBind(), bind to a new, fresh, empty item added via .addItem(), in order to create record and store it in the database.

Does this still applies for a bug?

I think it might, although I’m not the final arbiter. I can at least think of a real life use case where I’m ok with the default values of a (short) form.

-Olli