To achieve this you can use a4j:support
to attach ajax submission on a specific event that occur on the checkbox. e.g. onclick.
Here is a simple example:
Bean
public class TestBean {
private boolean chkBoxChecked;
public boolean isChkBoxChecked() {
return chkBoxChecked;
}
public boolean isBtnDisabled() {
return !this.chkBoxChecked;
}
public void setChkBoxChecked(boolean chkBoxChecked) {
this.chkBoxChecked = chkBoxChecked;
}
}
XHTML
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
template="/WEB-INF/template/default.xhtml">
<ui:define name="content">
<h:form id="frmTest">
<h:selectBooleanCheckbox id="chkBoolean" value="#{testBean.chkBoxChecked}">
<a4j:support event="onclick" ajaxSingle="true" reRender="btnSubmit"/>
</h:selectBooleanCheckbox>
<h:commandButton id="btnSubmit" value="Submit" disabled="#{testBean.btnDisabled}"/>
</h:form>
</ui:define>
</ui:composition>
Or use the client approach without submission:
XHTML
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
template="/WEB-INF/template/default.xhtml">
<ui:define name="head">
<script type="text/javascript">
window.onload = function() {
btnSubmit = document.getElementById('btnSubmit');
btnSubmit.disabled = #{testBean.btnDisabled};
}
</script>
</ui:define>
<ui:define name="content">
<h:form id="frmTest" prependId="false">
<h:selectBooleanCheckbox id="chkBoolean"
onclick="btnSubmit.disabled = !this.checked;"
value="#{testBean.chkBoxChecked}"/>
<h:commandButton id="btnSubmit" value="Submit"/>
</h:form>
</ui:define>
</ui:composition>
In this case the disabled
attribute should not be set on the h:commandButton
. Otherwise, changes on the client side using js will no affect the JSF tree.