andPat andPat - 1 year ago 63
Java Question

Get radio button choice value in Ajax Behavior

How can I get radio button choice "live" in wicket? I mean if I've a radio button with two choices A and B and I want to add an ajax behavior (on change, on update, whatever works) to the button so that if I chose A it happens something in the page, if I chose B happens something else. Can You help me?

Answer Source

Just add an AjaxFormChoiceComponentUpdatingBehavior to the RadioChoice:

RadioChoice radioChoice = new RadioChoice("myRadio", choiceList);
radioChoice.add( new AjaxFormChoiceComponentUpdatingBehavior("onchange") { 
    protected void onUpdate(AjaxRequestTarget target) {
        // Ajax actions here
        System.out.println("The selected value is " + getComponent().getDefaultModelObjectAsString()));
}  );

The Model of the RadioChoice will already be updated when executing onUpdate. Remember to add to target all components that are to be refreshed via AJAX (and of course to set setOutputMarkupId(true) to them).

There's an online example with source code of what you're looking for here. The relevant source code file is this one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download