9 Replies Latest reply on Jul 1, 2010 12:22 AM by Kapil Nimje

    How to Generate Rich PickList Control Dynamically

    Kapil Nimje Newbie

      Hi

       

      I am traying to generate the rich pick list control dynamically using Custom Java Classes i.e. HtmlPickList class when clicking on CommadButton, I am having the problem with how can I add the Items in the PickList, I need to display the predefined set  of questions from the database on left hand side of pick list and  selection may appear on right hand side of picklist..

       

       

      My code is is

       

           UIDiv demoBlockPanel = new UIDiv();
                  HtmlTab tab = new HtmlTab();
                  tab.setId("DemoQuestion");
                  tab.setLabel("Demo Questions");

       

                  HtmlPickList pickList = new HtmlPickList();
                     pickList.setValueExpression("value",application.getExpressionFactory().createValueExpression(context.getELContext(),


                     demoBlockPanel.getChildren().add(pickList);

       

      Kindly help me to sort out this problem..

        • 1. Re: How to Generate Rich PickList Control Dynamically
          Ilya Shaikovsky Master

          create UISelectItems and fill the value with the SelectItem objects.

          1 of 1 people found this helpful
          • 2. Re: How to Generate Rich PickList Control Dynamically
            Kapil Nimje Newbie

            Thanks for help, It's work for me now, But How do I set the dynamic values for HtmlPickList control, I am using below approach to set the list of selected values in demoQuestion list object, also my converter code not working when I setting the converter object as mentioned in below sample code..

             

            java.lang.NumberFormatException - For input string: "null"

             

                  UIDiv demoQuestionsDiv = new UIDiv();
                                demoQuestionsDiv.getAttributes().put("style","clear:both;margin-top:5px;float:left;padding:3px");
                                HtmlPickList pickList = new HtmlPickList();

                                pickList.setValueExpression("value",application.getExpressionFactory().createValueExpression(context.getELContext(), "#         {projectBean.questions[" + i + "].demoQuestions}" , String.class));
                                pickList.setStyleClass("DemoQuestionList");

                                DemoBlockConverter converter = new DemoBlockConverter();
                                pickList.setConverter(converter);

                                List<DemoQuestion> demoQuestionList = demoQuestionService.getEnabledDemoQuestions();
                                List<UISelectItem> demoQuestionSelectItemList = new ArrayList<UISelectItem>();
                                for (DemoQuestion demoQuestion : demoQuestionList) {
                                    UISelectItem items  = new UISelectItem();
                                  
                                    items.setId("DemoQuestion"+demoQuestion.getId());
                                    items.setItemValue(demoQuestion.getId());
                                    items.setItemLabel(demoQuestion.getText());

             

                                    demoQuestionSelectItemList.add(items);

                                  pickList.getChildren().add(items);
                               }

                          demoQuestionsDiv.getChildren().add(pickList);

             

             

            @Service(value = "demoBlockConverter")
            public class DemoBlockConverter implements Converter{

                @Autowired
                private DemoQuestionService demoQuestionService;

                public Object getAsObject(FacesContext context, UIComponent component, String value)
                        throws ConverterException {
                    System.out.println("INside Converter====="+value);
                    if (context == null || component == null) throw new NullPointerException();
                    if (value == null || value.length() == 0) return null;

                    DemoQuestion demoQuestion = demoQuestionService.getDemoQuestion(new Long(value));

                    return demoQuestion;
                } // end method getAsObject

                /*public List<DemoQuestion> getAsObject(FacesContext context, UIComponent component, Stri){

                } */

                public String getAsString(FacesContext context, UIComponent component, Object value)
                        throws ConverterException {
                    if (context == null || component == null) throw new NullPointerException();
                    if (value == null || value.getClass() != DemoQuestion.class) return null;

                    DemoQuestion demoQuestion = (DemoQuestion) value;
                    return demoQuestion.getId().toString();
                } // end method getAsString
            }

             

            Kindly let me know where I made the mistake...

            • 3. Re: How to Generate Rich PickList Control Dynamically
              Ilya Shaikovsky Master
              java.lang.NumberFormatException - For input string: "null"

               

              where this appears?

              1 of 1 people found this helpful
              • 4. Re: How to Generate Rich PickList Control Dynamically
                Kapil Nimje Newbie

                Hi.

                 

                This exception will be comming when i am selecting the values (Question) from the left hand side of pickList and hitting the Submit button. I realize that the value coming to the converter method i.e. getAsObject() is null, So my backing bean property is causing problem.

                • 5. Re: How to Generate Rich PickList Control Dynamically
                  Kapil Nimje Newbie

                  also please find the below server  exception log

                   

                  javax.faces.FacesException: javax.faces.FacesException: Exception while validating component with path : {Component-Path : [Class: org.ajax4jsf.component.AjaxViewRoot,ViewId: /app/project/new.xhtml][Class: javax.faces.component.html.HtmlForm,Id: newProjectForm][Class: com.sun.facelets.tag.ui.ComponentRef,Id: j_id57][Class: com.sun.facelets.tag.ui.ComponentRef,Id: j_id119][Class: org.ajax4jsf.component.html.HtmlAjaxRegion,Id: j_id147][Class: org.richfaces.component.html.HtmlTabPanel,Id: questionnaire_tab_panel][Class: org.richfaces.component.html.HtmlTab,Id: Q2][Class: com.dimestore.vi.web.component.UIDiv,Id: Q2Panel][Class: com.dimestore.vi.web.component.UIDiv,Id: j_id435][Class: com.dimestore.vi.web.component.UIDiv,Id: j_id442][Class: org.richfaces.component.html.HtmlPickList,Id: j_id405]}
                       at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:166)
                       at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:293)
                       at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:172)
                       at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:293)
                       at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:172)
                       at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:293)
                       at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:172)
                       at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:293)
                       at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:172)
                       at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:293)
                       at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:224)
                       at org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:455)
                       at org.apache.myfaces.lifecycle.ProcessValidationsExecutor.execute(ProcessValidationsExecutor.java:32)
                       at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103)
                       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
                       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:151)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                       at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                       at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
                       at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
                       at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
                       at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.providers.anonymous.AnonymousProcessingFilter.doFilterHttp(AnonymousProcessingFilter.java:105)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:277)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.concurrent.ConcurrentSessionFilter.doFilterHttp(ConcurrentSessionFilter.java:99)
                       at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                       at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                       at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
                       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
                       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at com.dimestore.vi.web.filter.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:41)
                       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
                       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
                       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
                       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
                       at java.lang.Thread.run(Thread.java:619)
                  Caused by: javax.faces.FacesException: Exception while validating component with path : {Component-Path : [Class: org.ajax4jsf.component.AjaxViewRoot,ViewId: /app/project/new.xhtml][Class: javax.faces.component.html.HtmlForm,Id: newProjectForm][Class: com.sun.facelets.tag.ui.ComponentRef,Id: j_id57][Class: com.sun.facelets.tag.ui.ComponentRef,Id: j_id119][Class: org.ajax4jsf.component.html.HtmlAjaxRegion,Id: j_id147][Class: org.richfaces.component.html.HtmlTabPanel,Id: questionnaire_tab_panel][Class: org.richfaces.component.html.HtmlTab,Id: Q2][Class: com.dimestore.vi.web.component.UIDiv,Id: Q2Panel][Class: com.dimestore.vi.web.component.UIDiv,Id: j_id435][Class: com.dimestore.vi.web.component.UIDiv,Id: j_id442][Class: org.richfaces.component.html.HtmlPickList,Id: j_id405]}
                       at javax.faces.component.UIInput.validate(UIInput.java:420)
                       at javax.faces.component.UISelectMany.validate(UISelectMany.java:401)
                       at javax.faces.component.UIInput.processValidators(UIInput.java:185)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:726)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:726)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:726)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:726)
                       at org.richfaces.component.UISwitchablePanel.processValidators(UISwitchablePanel.java:236)
                       at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:726)
                       at org.ajax4jsf.component.UIAjaxRegion.processValidators(UIAjaxRegion.java:125)
                       at org.ajax4jsf.component.AjaxViewRoot$3.invokeContextCallback(AjaxViewRoot.java:439)
                       at org.ajax4jsf.component.ContextCallbackWrapper.invokeContextCallback(ContextCallbackWrapper.java:44)
                       at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:163)
                       ... 69 more
                  Caused by: java.lang.NumberFormatException: For input string: "null"
                       at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                       at java.lang.Long.parseLong(Long.java:403)
                       at java.lang.Long.<init>(Long.java:671)
                       at com.dimestore.vi.web.convert.DemoBlockConverter.getAsObject(DemoBlockConverter.java:34)
                       at org.ajax4jsf.util.SelectUtils.getConvertedUISelectManyValue(SelectUtils.java:199)
                       at org.richfaces.renderkit.PickListRenderer.getConvertedValue(PickListRenderer.java:141)
                       at javax.faces.component.UISelectMany.getConvertedValue(UISelectMany.java:411)
                       at javax.faces.component.UIInput.validate(UIInput.java:399)
                       ... 81 more
                  
                  • 6. Re: How to Generate Rich PickList Control Dynamically
                    Ilya Shaikovsky Master

                    So seems happens there new Long(value) as value can't be parsed as long.

                    • 7. Re: How to Generate Rich PickList Control Dynamically
                      Kapil Nimje Newbie

                      So thats why I was asking , I don;t know how to set the dynamic values for HtmlPickList items,,, So could please let me know How do I set the values for selected item in HtmlPickList.. Was my approach wrong for setting the bean value using setValueExpression () method...

                       

                      Kindly help me..

                       

                      Thanks

                      Kapil

                      • 8. Re: How to Generate Rich PickList Control Dynamically
                        Kapil Nimje Newbie

                        Hi,

                          any update on below issues,

                        will appreciate for your reply....

                         

                        Thanks

                        • 9. Re: How to Generate Rich PickList Control Dynamically
                          Kapil Nimje Newbie

                          Hi Thanks, Now its working properly,

                           

                          Now I need to display the Panel right side of the dynamic picklist which shows the selected questions and there answers...

                          So how could I display the Panel Dynamically when selected it from the Pick List,

                           

                          I want it to be display in the following style

                           

                          My structure of database is as follows which display the list of "Question" in left side of Dynamic PickList and selection will appear in right side of list.

                           

                          Question(id, text)

                          Answer(id, text, questio_id)

                           

                          Question (1, "What is your Sex?)

                          Answer (1, "Select One", 1)

                          Answer (2, "Male" ,1)

                          Answer (3, "Female",1)

                           

                          Question (2, "What is your Age?)

                          Answer (4, "Select One", 2)

                          Answer (5, "10-20" ,2)

                          Answer (6, "20-30",2)

                           

                          Please find the attached screen shot for your reference.