Get selected row (single/multiple) from POJO based table in ADF

Sharing is Caring

Previously i have posted about populating af:table from managed bean using POJO and adding records in it, Check this-
Populate af:table programmatically from managead bean using POJO 
In this post i am extending previous post application
This post is about getting selected row from POJO based table , It is a common requirement while using af:table based on POJO

Get single row using custom selection listener-

SelectionListener  handles selection event of table , whenever user selects a row in table ,selection listener is fired
Set table RowSelection property to single (to select one row at a time) or multiple (to select multiple row ) and create a custom selection listener in managed bean that will handle table’s selection event

 

See selection listener code in managed bean we can get selected row  using this –

import oracle.adf.view.rich.component.rich.data.RichTable;
import org.apache.myfaces.trinidad.event.SelectionEvent;

/**Method to get selected row(single)
* @param selectionEvent
*/
public void tableSelection(SelectionEvent selectionEvent) {
//Get table from selectionEvent
RichTable richTable = (RichTable) selectionEvent.getSource();
//Cast to the List that populates table
PersonBean row = (PersonBean) richTable.getSelectedRowData();
//Get the attributes (column) from list
System.out.println(row.getName());
}

Now check this –

Output on console 🙂

 

Get single/multiple selected row on a button click (ActionEvent)-

Set table RowSelection to multiple and select multiple row using Ctrl key of your keyboard
and check this code to get multiple selected row using RowKeySet, We can get all row using getSelectedRowKeys method

 

import org.apache.myfaces.trinidad.model.RowKeySet;
import java.util.Iterator;

/**Method to get all selected record in af:table
* @param actionEvent
*/
public void getSelectedRecord(ActionEvent actionEvent) {
//getTableBind is binding of table on page.
RowKeySet selectedEmps = getTableBind().getSelectedRowKeys();
//Create iterator from RowKeySet
Iterator selectedEmpIter = selectedEmps.iterator();

while (selectedEmpIter.hasNext()) {
String i = selectedEmpIter.next().toString();
//personList is the list used to populate table and name is a column of table
//So here filter list using index and get values then
PersonBean rowIter = personList.get(Integer.parseInt(i));
System.out.println(rowIter.getName());
}

}

Now run and check again –

Output on console 🙂

Sample ADF Application- Download
Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

12 thoughts on “Get selected row (single/multiple) from POJO based table in ADF”

  1. Hi. I tried the code to get selected rowes in button click, but there are the following three errors.

    1) Method getTableBind() is not found
    2) Type PersonBean not found
    3) Type or Variable personList not found.

    What changes should I make or libraries should I include to fix this issue??

  2. I did this:
    public void seleccionarEvento(SelectionEvent selectionEvent) {
    List event = (List)Util.getValuePageFlowScope("eventos");
    RichListView lst = (RichListView)selectionEvent.getComponent();
    RowKeySet rowKey = lst.getSelectedRowKeys();
    int sel =(Integer)rowKey.toArray()[0];
    Eventos.Evento evento=(Eventos.Evento) event.get(sel);
    Util.setValuePageFlowScope("eventoSeleccionado", evento);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *