Tuesday, April 27, 2010

How to set focus using Javascript and Java

There are scenarios when we want to focus on a particular UI component.
There are many possibilities but here i explain some of the basic cases.

Note: You have to set clientComponent="true" for the component to be focused.
Case 1: Default focus when a page is rendered

For this case we can set the initialFocusId for page document where pt1:it2 is the absolute id.

................

Case 2: Using JavaScript after Page is rendered (on some action)

function customScript(evt) {
var source = evt.getSource();
var it = source.findComponent("it1");
//var it = AdfPage.PAGE.findComponentByAbsoluteId("pt1:it1");

it.focus();
}

Case 3: Using java in managed bean

-- Java method goes here

public void TestFocus(ActionEvent actionEvent) {
// Add event code here...

FacesContext context = FacesContext.getCurrentInstance();
String textId = "pt1:it1"; //popup.getClientId(context);
StringBuilder script = new StringBuilder();
script.append("var iptext = AdfPage.PAGE.findComponent('").append(textId).append("'); ").append(" iptext.focus() ");
ExtendedRenderKitService erks =
Service.getService(context.getRenderKit(),
ExtendedRenderKitService.class);
erks.addScript(context, script.toString());


}

1 comment:

  1. Productivity is never an accident. It is always the result of a commitment to excellence, intelligent planning, and focused effort. See the link below for more info.


    #focused
    www.ufgop.org

    ReplyDelete