Please disable your adblock and script blockers to view this page

Search this blog

Monday, 16 May 2016

How to queue SelectionEvent programmatically, Call af:table Selection Listener programmatically

Hello All
Hope you all are doing good :) 
Previously I have posted about defining custom selection listener for af:table and perform desired operation on selection event of table row

Selection Listener is fired when server receives a selection event from client means whenever user selects a row in table (Selection Event) then server process that event and execute selection listener defined in managed bean

And this post is about creating selection event programmatically without user interaction with table.
In ADF we can queue ActionEvent , ValueChangeEvent and in same manner we can create and queue SelectionEvent programmatically

So for this I have Created a Fusion Web Application and prepared model using Departments table of HR Schema

Dropped Departments ViewObject on page as af:table and defined custom selection listener of af:table in managed bean

Packages Used-

import java.util.ArrayList;
import java.util.List;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Key;
import oracle.jbo.Row;

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.RowKeySetImpl;

Code for af:table Selection Listener-

     * Programmatic evaluation of EL.
     * @param el EL to evaluate
     * @return Result of the evaluation
    public static Object evaluateEL(String el) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
        ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);

        return exp.getValue(elContext);

     * Programmatic invocation of a method that an EL evaluates to.
     * @param el EL of the method to invoke
     * @param paramTypes Array of Class defining the types of the parameters
     * @param params Array of Object defining the values of the parametrs
     * @return Object that the method returns
    public static Object invokeEL(String el, Class[] paramTypes, Object[] params) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
        MethodExpression exp = expressionFactory.createMethodExpression(elContext, el, Object.class, paramTypes);
        return exp.invoke(elContext, params);

    /**Custom Selection Listener for Department Table
     * @param selectionEvent
    public void deptTabCustomSelectionListener(SelectionEvent selectionEvent) {
        //To make selected row as Current
        invokeEL("#{bindings.Departments1.collectionModel.makeCurrent}", new Class[] { SelectionEvent.class }, new Object[] {
                 selectionEvent });
        // Get the selected row , by this you can get any attribute of that row
        Row selectedRow = (Row) evaluateEL("#{bindings.Departments1Iterator.currentRow}");
        System.out.println("SELECTED DEPARTMENT IS- " + selectedRow.getAttribute("DepartmentName"));

Dropped a button on page to call Next Operation and execute selection listener programmatically

Here I am calling default Next operation on af:table to select next row in rowset programmtically
and as user is not selecting row manually so selection listener will not execute that's why we have to create and queue selection event programmatically
Now Bind af:table to managed bean and write code on button ActionListener to queue table selection listener

    //Component Binding of Department Table
    private RichTable deptTableBind;

    public void setDeptTableBind(RichTable deptTableBind) {
        this.deptTableBind = deptTableBind;

    public RichTable getDeptTableBind() {
        return deptTableBind;

    public QueueSelectionListenerBean() {

    /**Method to return BindingContainer
     * @return
    public BindingContainer getBindingsCont() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();

     * Generic Method to get OperationBinding
     * */
    public OperationBinding executeOperation(String operation) {
        OperationBinding createParam = getBindingsCont().getOperationBinding(operation);
        return createParam;

    /**Method to queue table selection listener programmatically
     * @param actionEvent
    public void callSelectionListenerAction(ActionEvent actionEvent) {
        try {
            DCIteratorBinding deptIter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
            Row currentRow = deptIter.getCurrentRow();
            //List to store table RowKey
            List rowKeyList = new ArrayList();
            if (currentRow != null) {

                //Add current row primary key to RowKeyList to form Old Key List
                Key jboKey = new Key(new Object[] { currentRow.getAttribute("DepartmentId") });

                RowKeySet newRks = new RowKeySetImpl();
                //Create SelectionEvent Object and put necessary values
                SelectionEvent selectionEvent =
                    new SelectionEvent(deptTableBind.getSelectedRowKeys(), newRks, deptTableBind);
                //Queue Selection Event
        } catch (Exception e) {

Now run and check application , Click on Move Next button and see current row is changed to Marketing

and here custom selection listener prints selected Department Name

Sample ADF Application -Download
Cheers :) Happy Learning

No comments :

Post a Comment