The blog for hints & tips about Oracle Application Development Framework and Mobile Application Framework

  • ADFv
    November 22, 2011

Creating ADF Faces Comamnd Button at Runtime

Frank Nimphius
Master Principal Product Manager

In ADF Faces, the command button is an instance of RichCommandButton and
can be created at runtime. While creating the button is not difficult at all,
adding behavior to it requires knowing about how to dynamically create and add
an action listener reference. The example
code below shows two methods: The first method, handleButtonPress is a public method exposed on a managed bean.

public void handleButtonPress(ActionEvent event){ 
System.out.println("Event handled"); 
//optional: partially refresh changed components if command 
//issued as a partial submit

The second method is called in response to a user
interaction or on page load and dynamically creates and adds a command button. When
the button is pressed, the managed bean method – the action handler – defined above
is called. The action handler is referenced using EL in the created MethodExpression instance. If the
managed bean is in viewScope, backingBeanScope or pageFlowsScope, then you need
to add these scopes as a prefix to the EL (as you would when configuring the
managed bean reference at design time)

//Create command button and add it as a child to the parent component that is passed as an 
//argument to this method
private void reateCommandButton(UIComponent parent){
RichCommandButton edit = new RichCommandButton();
//make the request partial
  //compose the method expression to invoke the event handler
FacesContext fctx = FacesContext.getCurrentInstance();
  Application application = fctx.getApplication();
  ExpressionFactory elFactory = application.getExpressionFactory();
  ELContext elContext = facesCtx.getELContext();
  MethodExpression methodExpressio = null;
  //Make sure the EL expression references a valid managed bean method. Ensure
  //the bean scope is properly addressed 

methodExpression =  elFactory.createMethodExpression( 
                            Object.class,new Class[] {ActionEvent.class});
  //Create the command buttonaction listener reference
  MethodExpressionActionListener al = null;       
al= new MethodExpressionActionListener(methodExpression); 
//add new command button to parent component and PPR the component for 
//the button to show
AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance(); 

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.