0

Event listener for SelectManyCheckbox is not invoked when a checkbox is clicked.

My SelectManyCheckbox code:

    SelectManyCheckbox smcb = new SelectManyCheckbox();

    UISelectItem item = new UISelectItem();
    item.setItemValue("ItemValue");
    item.setItemLabel("ItemLabel");

    smcb.getChildren().add(item);

I have tried AjaxBehavior:

    AjaxBehavior ajaxBeh = (AjaxBehavior) fc.getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
    ajaxBeh.setRender(Collections.singletonList("@this"));
    ajaxBeh.setExecute(Collections.singletonList("@this"));
    ajaxBeh.setImmediate(true);
    ajaxBeh.setTransient(true);
    ajaxBeh.addAjaxBehaviorListener(new AjaxBehaviorListener() {
        @Override
        public void processAjaxBehavior(AjaxBehaviorEvent e)
                throws AbortProcessingException {
            System.out.println("Event Triggered");
        }
    });

    smcb.addClientBehavior("change",ajaxBeh);

and also ValueChangeListener:

    public class CustomValueChangeListener implements ValueChangeListener {
    @Override
    public void processValueChange(ValueChangeEvent arg0)
           throws AbortProcessingException {
        System.out.println("processValueChange");
        }
    }

    smcb.addValueChangeListener(new CustomValueChangeListener ());

Both of the above methods do not work.

How can I setup an event listener so that it gets called when a checkbox is selected or unselected?

1
  • Why do you need to do it in Java
    – Kukeltje
    Commented Jul 19, 2017 at 16:41

1 Answer 1

1

I think you could very well try to run an ajax event from the jsf code by calling a valueChangeListener and work on your selection at the Bean end.

JSF Code:
<p:selectManyCheckbox id="custom" columns="1" layout="pageDirection" value="#{dapBean.selection}" disabled="#{!dapBean.status}"
                    valueChangeListener="#{dapBean.checkListener}">
    <f:selectItems value="#{dapBean.toolOptions}" var="tool" itemLabel="#{tool.toolLabel}" itemValue="#{tool.toolLabel}" />
    <f:ajax event="valueChange" update = "@form"></f:ajax>
</p:selectManyCheckbox>

This can be backed up with a valueChangeListener at the back end.

Java Code:
public void checkListener(ValueChangeEvent e) {
    System.out.println(e.getNewValue().toString());
    System.out.println(e.getOldValue().toString());

}

Everytime you check or uncheck a checkBox you can get your selected as well as unchecked entries.

Hope it helps!!

1
  • Thanks for the suggestion. I have already used ValueChangeListener as well ajax events listeners for SelectManyCheckbox which are defined in jsf but in this case (because dynamic nature of data), I have to create SelectManyCheckbox in backing bean. Shouldn't it be possible to use ValueChangeListener and ajax events for SelectManyCheckbox created in backing beans?
    – amah
    Commented Jul 19, 2017 at 16:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.