Again a post in series of Working with ADF Faces Components programmatically
Previously i have posted about Getting value , Applying Action Listener , Applying Client/Server Listener, Creating and applying client Attributes, Setting value expression , Applying Validation to programmatically created component
Now this post post is about applying Value Change Listener to a component that is created at run time
See how to do this (Jdev 12.1.3)-
Previously i have posted about Getting value , Applying Action Listener , Applying Client/Server Listener, Creating and applying client Attributes, Setting value expression , Applying Validation to programmatically created component
Now this post post is about applying Value Change Listener to a component that is created at run time
See how to do this (Jdev 12.1.3)-
- First created a FusionWebApplication and a page in viewController project
- Dropped a button on page , on this button action i will create an inputText programmatically and assign Value Change Listener method reference to it
- To create new inputText i have added following code (described in previous post)
- Next step is to create a Value Change Listener method in managed bean, that will be called on value change event of inputText
- Now assign this Value Change Listener method to programmatically created inputText, see how to do this
Helper method to resolve Value Change Listener(javax.faces.event.ValueChangeListener) - All done :) Run and check application , On button click an inputText is created, i have entered a value see Value Change Listener is called
/**Method to add dynamically created component to a parent layout * @param parentUIComponent * @param childUIComponent */ public void addComponent(UIComponent parentUIComponent, UIComponent childUIComponent) { parentUIComponent.getChildren().add(childUIComponent); AdfFacesContext.getCurrentInstance().addPartialTarget(parentUIComponent); }
//Code to create af:inputText and add it to panelgroup layout RichInputText ui = new RichInputText(); ui.setId("pit1"); ui.setContentStyle("width:200px;color:navy"); //getParentGroupLayoutBind is the component binding of parent panel group layout addComponent(getParentGroupLayoutBind(), ui);
/**ValueChangeListener method to be applied on programmatically created inputText * @param valueChangeEvent */ public void testValuChangeListener(ValueChangeEvent valueChangeEvent) { FacesMessage msg = new FacesMessage("**VALUE CHANGE LISTENER CALLED** " + valueChangeEvent.getNewValue()); FacesContext.getCurrentInstance().addMessage(null, msg); }
//Packages used import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.faces.application.Application; import javax.el.MethodExpression; import javax.faces.context.FacesContext; import javax.faces.component.UIComponent; import javax.faces.event.MethodExpressionValueChangeListener;
/**Method to to resolve Value Change Listener method * @param actionName */ private ValueChangeListener resolveValueChangeListener(String validatorName) { //ValueChangeListener method takes 1 argument of following type , we have to define that Class[] argtypes = new Class[1]; argtypes[0] = ValueChangeEvent.class; FacesContext facesCtx = FacesContext.getCurrentInstance(); Application app = facesCtx.getApplication(); ExpressionFactory elFactory = app.getExpressionFactory(); ELContext elContext = facesCtx.getELContext(); MethodExpression methodExp = elFactory.createMethodExpression(elContext, validatorName, null, argtypes); return new MethodExpressionValueChangeListener(methodExp); }
To apply this Value Change Listener to inputText just write this - Pass exact el reference of bean value change listener method to resolveValueChangeListener method
//Pass string that contains proper el reference of Validator method ui.addValueChangeListener(resolveValueChangeListener("#{viewScope.Testbean.testValuChangeListener}"));
No comments :
Post a Comment