Set focus on a component with Apache Wicket?

vagabond picture vagabond · Apr 13, 2010 · Viewed 14.5k times · Source

How do you set focus on a component with Apache Wicket? Searching leads to very little information, mostly on setting the default field. I do not want to set a default field, rather I am looking to set focus when, for example, a specific radio button is selected.

Answer

martin-g picture martin-g · Apr 10, 2012

I suggest using the native org.apache.wicket.ajax.AjaxRequestTarget#focusComponent(). For example:

/**
 * Sets the focus in the browser to the given component. The markup id must be set. If            
 * the component is null the focus will not be set to any component.
 * 
 * @param component
 *            The component to get the focus or null.
 */
 org.apache.wicket.ajax.AjaxRequestTarget#focusComponent(Component component)