3

I'm trying to do some action in the ManagedBean based on the state of the checkbox.

Checkbox is here:

<p:selectBooleanCheckbox id="cb">
    <p:ajax listener="#{myBB.checkboxChanged}"/>
</p:selectBooleanCheckbox>

The signature of the listener method is this:

public void checkboxChanged(AjaxBehaviorEvent event)

The listener method is called as expected, but I'm not sure how to get the state of the checkbox(if it is selected) This is false all the time:

((SelectBooleanCheckbox)event.getSource()).isSelected()

Do I need to bind the value of the checkbox? I'd like to avoid that, because the checkboxes are generated from enum in a loop and I don't have any boolean where to bind it(the model is designed in a way that I'm adding/removing the instances of enum to a collection, not setting boolean properties)

I'm running the code on Tomcat 6.0.36 with Primefaces 3.5

JSF implementation is Apache Myfaces 2.1.10

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76

2 Answers2

2

Your code works as expected for me on Mojarra 2.2.1 and PrimeFaces 4.0RC1 as well as Mojarra 2.0.3 and PrimeFaces 3.5. Unfortunately I didn't have 2.1 handy. What version of PrimeFaces are you using? Can you post your complete code?

LucasP
  • 1,665
  • 16
  • 24
  • I'm using primefaces 3.5 and deploying on Tomcat 6.0.36. What works? The isSelected() gives you correct value? Oops... I'm not using JSF 2.1 as I thought so. – NeplatnyUdaj Sep 18 '13 at 14:35
  • isSelected() correctly alternates between true/false when the checkbox is clicked on my test environment. I am deploying on Tomcat 7.0.34. – LucasP Sep 18 '13 at 15:03
  • So it looks like it's a problem of apache myfaces. – NeplatnyUdaj Sep 18 '13 at 15:16
2

Lucas was right, everything is fine. The problem is I did not put the button in the form.

After adding <h:form> tag, everything is ok.

NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
  • It is always helpful to create an MCV Example. Otherwise other people would assume, that you have a form. – alexander Dec 09 '15 at 14:38