5 Replies Latest reply: Jan 2, 2008 12:05 PM by Lucas Jellema RSS

    cursor position


      i have two <af:inputText> and a command button in my page.
      when the page is loaded i want the cursor to be in the first textbox and when i click a button the cursor should be focused to the next input text.

      how to do this??
      plz help me.
        • 1. Re: cursor position
          Lucas Jellema

          You asked this before - and I provided a reply. Does this not work for you?

          You probably could add an onLoad listener (JavaScript) to the page, that will set the focus once the page has loaded. It could take the name of the item to focus on from some hidden field that you may create using an hidden inputText whose value is derived from a managed bean property set in the action listener.

          If the focus is to be set after a PPR request/response was processed, you may be interested in the option of adding JavaScript to the PPR response (I found it in the ADF 11g Web User Interface Developer's Guide). Using the ExtendedRenderKitService you apparently can add JavaScript inside the action listener logic.

          • 2. Re: cursor position
            thankx a lot for the replies.

            can u just provide me a an example to see how focus is set to the component using java script.

            i tried a lot of examples but didnt succeed.

            • 3. Re: cursor position
              Lucas Jellema
              Can you provide me with the code of your page and your attempt to set the focus using JavaScript on the onLoad and I will try to take it from there.

              • 4. Re: cursor position
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                <%@ page contentType="text/html;charset=windows-1252"%>
                <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
                <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
                <%@ taglib uri="http://xmlns.oracle.com/adf/faces/rich" prefix="af"%>


                <af:document binding="#{backing_setFocus.document1}" id="document1"
                <script language="JavaScript" type="text/javascript">
                function setFocus(event) {      
                var t=AdfPage.PAGE.findComponent("inputText2");

                <af:form binding="#{backing_setFocus.form1}" id="form1">
                <af:inputText label="Name" binding="#{backing_setFocus.inputText1}"
                <af:inputText label="ID " binding="#{backing_setFocus.inputText2}"
                <af:commandButton text="Set focus"
                actionListener="#{backing_setFocus.on_click}" >
                <af:clientListener method="setFocus" type="action"/>



                when i run this the loading image is seen.but in the status bar it is showing done.
                when i remove the script part and <af:clientListener> part from the code the page is loaded and i can see the page.

                plz help me
                • 5. Re: cursor position
                  Lucas Jellema
                  Dear KK,

                  I have done some tests - and the results were not all extremely satisfying. To say the least. However, I did find a method that will probably do it for you.

                  It involves a little bit of JavaScript - not using the AdfPage API, as I did not get it to work properly.

                  The client now looks like this:
                  <?xml version='1.0' encoding='windows-1252'?>
                  <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"
                    <jsp:directive.page contentType="text/html;charset=windows-1252"/>
                      <af:document initialFocusId="inputText1">
                        <trh:script text="function setFocus(event) { 
                           var t=document.getElementById('inputText2::content');
                        function setFocusId(id) { 
                          var t=document.getElementById(id);
                        <af:form id="form1">
                            <af:inputText label="Name" id="inputText1"/>
                            <af:inputText label="ID " id="inputText2"/>
                          <af:commandButton text="Set focus on ID field" id="commandButton1"
                                            actionListener="#{backing_setFocus.commandButton1}" />
                          <af:commandButton text="Set focus on ID field " id="commandButton3"
                            <af:clientListener method="setFocus" type="action"/>
                  The associated backing bean has the commandButton1 method that is called as actionListener. In addition to doing whatever you want it to do, it writes a little piece of JavaScript to be executed when the PPR completes. It relies on the fact that an inputText element is rendered using a table with the same id as the inputText and inside that table an HTML input element with id equals tableId::content.
                  package nl.amis.adffaces;
                  import javax.faces.component.UIComponent;
                  import javax.faces.context.FacesContext;
                  import javax.faces.event.ActionEvent;
                  import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
                  import org.apache.myfaces.trinidad.util.Service;
                  public class SetFocusBacker {
                      public SetFocusBacker() {
                      public void commandButton1(ActionEvent actionEvent) {
                          FacesContext facesContext = FacesContext.getCurrentInstance();
                          ExtendedRenderKitService service =
                          UIComponent ip = facesContext.getViewRoot().findComponent("inputText2");
                          String clientId = ip.getClientId(facesContext);
                          System.out.println("added client side script "+clientId);
                  I hope this does what you are looking for - or at least helps you find a way yourself.

                  best regards,