Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set focus on a component with Apache Wicket?

Tags:

java

wicket

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.

like image 488
vagabond Avatar asked Apr 13 '10 19:04

vagabond


2 Answers

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)
like image 152
martin-g Avatar answered Oct 31 '22 11:10

martin-g


Once you create your behavior to set the focus, you should be able to add it to the component on any event, just make sure that component is part of the AjaxRequestTarget. I don't see why this wouldn't work...

myRadioButton.add(new AjaxEventBehavior("onchange") {
 @Override
 protected void onEvent(AjaxRequestTarget target) {
    myOtherComponent.add(new DefaultFocusBehavior());
        target.addComponent(myForm);
 }
});

Here's a link that shows how to create the default focus behavior if you do not have one already: http://javathoughts.capesugarbird.com/2009/01/wicket-and-default-focus-behavior.html

like image 37
schmimd04 Avatar answered Oct 31 '22 09:10

schmimd04