When a user enters a number in a Wicket text field, I need to capture the value entered on an AJAX onchange
event. How can I do this?
Asked
Active
Viewed 2.4k times
17

Ondra Žižka
- 43,948
- 41
- 217
- 277

sangameshwar
- 181
- 1
- 1
- 3
1 Answers
36
Use an OnChangeAjaxBehavior
, and your component model will be updated automatically. You can query the value by using component.getDefaultModelObject()
add(new TextField<String>(id, someModel)
.add(new OnChangeAjaxBehavior(){
private static final long serialVersionUID =
2462233190993745889L;
@Override
protected void onUpdate(final AjaxRequestTarget target){
// Maybe you want to update some components here?
// Access the updated model object:
final Object value = getComponent().getDefaultModelObject();
// or:
final String valueAsString =
((TextField<String>) getComponent()).getModelObject();
}
}));
Actually, OnChangeAjaxBehavior
is pretty aggressive because it registers a key listener, so in most cases new AjaxFormComponentUpdatingBehavior("onchange"){...}
will work just as well, if you just want the onchange
event.

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
-
7+1 for mentioning `AjaxFormComponentUpdatingBehavior`, because a typical error is to just add a "regular" `AjaxEventBehavior` to a form component and then wonder why the new value doesn't appear in the model. – biziclop Apr 06 '11 at 13:12