Search This Blog


Monday, October 17, 2011

ADF: How to programmatically set focus on an input component

The af:document component has a property InitialFocusId that allows you to define the Id of an input component that should take focus when a page initially loads. But how do you set the component focus for pages that are already rendered? A use case for this is a partial submit that executes logic on the server, after which the cursor focus in the UI needs to be set to a specific input component of a page or page fragment. The solution to this use case is JavaScript that is generated on the server and executed on the client.
The simplified sample I wrote contains of three input text fields and three command buttons. Initially, using theaf:document InitialFocusId property, the cursor focus is set to the first input text field.
The command button send a partial submit to a managed bean on the server. The managed bean reads the component Id of the input text field to put the focus to from a client attribute that is defined on each command button. Using the MyFaces Trinidad ExtendedRenderKitService class, the managed bean sends a JavaScript call to the client to change the current component focus.
Important: By default, ADF Faces does not render all its user interface components as JavaScript objects. It only renders those components as JavaScript objects that have behavior, like a table that can have its columns moved. Input text components, for example, are rendered in HTML only, which means that the ADF client side JavaScript framework will not find a handle to the component unless you tell ADF Faces to create one. For this, you set theClientComponent property of the input text fields to true. This also is required for the af:document InitialFocusIdproperty to work.
Below is the managed bean code that handles the command button action and that composes and executes the JavaScript to set the client side UI focus.

 As mentioned, the command buttons in the sample have a client attribute attached that I use to determine which input component should receive focus. The managed bean calls the client attribute here:
RichCommandButton rcb = (RichCommandButton)event.getSource();
String focusOn = (String)rcb.getAttributes().get("focusField");
 In the page definition, the client attribute is defined as
<af:commandButton text="Focus Field 2" id="commandButton1"
  <af:clientAttribute name="focusField" value="it2"/>
<af:commandButton text="Focus Field 3" id="cb1"
  <af:clientAttribute name="focusField" value="it3"/>
In your custom implementation of this use case, you for sure will have your own way of determining the next focus target. The server side code however doesn't change much.