Droid Developer
Droid Developer

Reputation: 29

primefaces p:selectBooleanCheckbox default checked state

I have a problem with this simple p:selectBooleanCheckbox component :

<p:selectBooleanCheckbox value="#{BackingBean.booleanFlag}"> 
      <p:ajax event="change" global="false" />
</p:selectBooleanCheckbox>

in the backing bean I have the default state of boolean variable booleanFlag = true inside @PostConstruct method. Why the setter method is called with the 'false' value ???? In this way seems impossible to have a default-checked checkbox...

This behaviour happens also (and only) with other "boolean state component", like the new pf 5.0 component p:inputSwitch. (e.g. the pf extension tristate checkbox has a string based state and it is working well)

Where is my fault ?

Upvotes: 1

Views: 22625

Answers (2)

cнŝdk
cнŝdk

Reputation: 32145

No, it's possible to have a default checked checkbox, simply initialize its value to true when you declare it in your backing bean:

private boolean booleanFlag = true;
public boolean isBooleanFlag() {
    return booleanFlag;
}

public void setBooleanFlag(boolean value) {
    this.booleanFlag = value;
}

And in your page:

<p:selectBooleanCheckbox value="#{BackingBean.booleanFlag}"> 
    <p:ajax event="change" global="false" />
</p:selectBooleanCheckbox>

Upvotes: 0

Droid Developer
Droid Developer

Reputation: 29

true means checked, while false means not checked....

private boolean booleanFlag = true; // DEFAULT CHECKED 
public boolean isBooleanFlag() {
    return booleanFlag;
}

public void setBooleanFlag(boolean value) {
    this.booleanFlag = value;
}

the problem is that the setBooleanFlag is called with the parameter value = false

Upvotes: 1

Related Questions