Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:08:20+00:00 2026-05-24T04:08:20+00:00

I am using JSF 2.0 and PrimeFaces. I have a selectOneMenu and a selectManyCheckbox.

  • 0

I am using JSF 2.0 and PrimeFaces. I have a selectOneMenu and a selectManyCheckbox. I want to achieve this scenario:

When an user chooses:

  • option1(PieChart), then the user is allowed to check max one checkbox
  • option2(BarChart), then the user is allowed to check max two checkboxes

Here is my attempt so far, the view:

<h:selectOneMenu value="#{bean.chartType}">
    <f:selectItem itemValue="PieChart" itemLabel="PieChart" />
    <f:selectItem itemValue="BarChart" itemLabel="BarChart" />
    <p:ajax event="keyup" update="checkbox" listener="#{bean.checkboxHandler}" />
</h:selectOneMenu>

<h:selectManyCheckbox id="checkbox" value="#{bean.selectedItemscheckbox}">
    <f:selectItem itemValue="priority" itemLabel="By priority" />
    <f:selectItem itemValue="project" itemLabel="By project" />
    <f:selectItem itemValue="issuetype" itemLabel="By IssueType" />
    <f:selectItem itemValue="group" itemLabel="By group" />
</h:selectManyCheckbox>

(note: I am not sure if the <p:ajax event> is correct)

The bean:

public void checkboxHandler() {
    Iterator it1 = selectedItemscheckbox.iterator();
    if (chartType.equals("PieChart")) {
        while (it1.hasNext()) {
            if (selectedItemscheckbox.size() > 1) {
                // Here should be somthing that shows a message to the user.
            }
        }
    }
}

After tring BalusC’s solution,this error occured :

java.lang.InstantiationException: DAOKPI.TestAjax$MyCheckboxValidator
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.faces.application.ApplicationImpl.newThing(ApplicationImpl.java:1699)
at com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:1557)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.createValidator(ValidatorTag     HandlerDelegateImpl.java:238)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyAttachedObject(Validato    rTagHandlerDelegateImpl.java:132)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyNested(ValidatorTagHand    lerDelegateImpl.java:205)
at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.apply(ValidatorTagHandlerDel    egateImpl.java:87)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandle     r.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDel    egateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:769)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

Update

Thank you problem solved 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:08:21+00:00Added an answer on May 24, 2026 at 4:08 am

    You need a custom Validator which throws a ValidatorException when the checkbox contains too many checked items. This Validator needs to be attached to the checkbox component and there should be a <h:message> or a <h:messages> component somewhere in the view which can display the validation error.

    The ajax event is wrong. Inside a dropdown, you would like to hook on the change event instead. Inside a checkbox or radio button you would like to hook on click event instead. Both which are already the default when you leave the event attribute to default. Each component should in turn ajax-submit themselves and the other one and finally re-render the message component to reflect the validation error.

    In order to figure the selected dropdown value inside the custom validator, the cleanest way would be to set the whole dropdown component as a custom attribute of the checkbox component. This can be achieved by binding the dropdown to the view by binding attribute and specify that as value of <f:attribute> inside the checkbox. The dropdown component and thus also its value can then be obtained inside the custom validator.

    Summarized, the below kickoff example should work (note that I used <f:ajax> as <p:ajax> adds no extra advantages in this particular case and I admittedly can’t tell from top of head how and if that would work):

    <h:selectOneMenu id="chartType" binding="#{chartType}" value="#{bean.chartType}">
        <f:selectItem itemValue="PieChart" itemLabel="PieChart" />
        <f:selectItem itemValue="BarChart" itemLabel="BarChart" />
        <f:ajax execute="chartType checkbox" render="checkboxmessage" />
    </h:selectOneMenu>
    <h:selectManyCheckbox id="checkbox" value="#{bean.selectedItemscheckbox}">
        <f:selectItem itemValue="priority" itemLabel="By priority" />
        <f:selectItem itemValue="project" itemLabel="By project" />
        <f:selectItem itemValue="issuetype" itemLabel="By IssueType" />
        <f:selectItem itemValue="group" itemLabel="By group" />
        <f:validator validatorId="myCheckboxValidator" />
        <f:attribute name="chartType" value="#{chartType}" />
        <f:ajax execute="chartType checkbox" render="checkboxmessage" />
    </h:selectManyCheckbox>
    <h:message id="checkboxmessage" for="checkbox" />
    

    With

    @FacesValidator("myCheckboxValidator")
    public class MyCheckboxValidator implements Validator {
    
        @Override
        public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
            UIInput chartTypeComponent = (UIInput) component.getAttributes().get("chartType");
            String chartType = (String) chartTypeComponent.getValue();
            List<String> selectedItemscheckbox = (List<String>) value;
    
            if ("PieChart".equals(chartType) && selectedItemscheckbox.size() > 1) {
                throw new ValidatorException(new FacesMessage("Max 1 item allowed"));
            }
            else if ("BarChart".equals(chartType) && selectedItemscheckbox.size() > 2) {
                throw new ValidatorException(new FacesMessage("Max 2 items allowed"));
            }
        }
    
    }
    

    Note that I assume the #{bean.selectedItemscheckbox} to be a List<String>. If it’s actually a String[], then you need to alter the validator code accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this jQuery snippet to create tooltips on some JSF primefaces elements in
I'm using JSF 2.0 and PrimeFaces library. I have a page with several inputs
I have a problem using JSF 2.0, PrimeFaces and the tuckey.org UrlRewriteFilter. The problem
Hi there Im just learning JSF 2 using Primefaces. I have a question regarding
I am Using primefaces and Jsf with glassfish when i try to execute this
I'm using JSF 2.0 and primefaces. I have one page with several inputs inside
I am using JSF 2.0 and PrimeFaces 2.2. I have a data table whose
I am using JSF 2.0 and PrimeFaces 2.2.1. In my template.xhtml, I have a
I'm using JSF 2 and PrimeFaces 2.1 on GlassFish. I have a page that
I have an application developed using Primefaces 2.2.1 and JSF 2.0, deployed on Glassfish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.