Question

I'm trying to make a RadioField, like (yes, no, unknown). For "yes" i have a textfield. I want that if anything is typed on the textfield, "yes" will be selected on the radiobutton. I tried this, but i'm getting an Exception:

size = new NumberField<Double>("size", fieldLabel("size"), HasUnits.MICROMETER);

size.add(new AjaxFormComponentUpdatingBehavior("onchange") {

    private static final long serialVersionUID =
        2462233190993745889L;

    @Override
    protected void onUpdate(final AjaxRequestTarget target) {
        downSet.setDefaultModelObject("yes");
    }
});
add(size);
size.setOutputMarkupId(true);

here is the exception i'm getting:

org.apache.wicket.WicketRuntimeException: Behavior redb.main.modules.sample.view.details.pck.DownSetPanel$2 can only be added to an instance of a FormComponent
    at org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior.onBind(AjaxFormComponentUpdatingBehavior.java:79) ~[wicket-core-6.8.0.jar:6.8.0]
    at org.apache.wicket.behavior.AbstractAjaxBehavior.bind(AbstractAjaxBehavior.java:68) ~[wicket-core-6.8.0.jar:6.8.0]
    at org.apache.wicket.Behaviors.add(Behaviors.java:59) ~[wicket-core-6.8.0.jar:6.8.0]
    at org.apache.wicket.Component.add(Component.java:4511) ~[wicket-core-6.8.0.jar:6.8.0]
    at redb.main.modules.sample.view.details.pck.DownSetPanel.onInitialize(DownSetPanel.java:61) ~[DownSetPanel.class:na]
    at org.apache.wicket.Component.fireInitialize(Component.java:863) ~[wicket-core-6.8.0.jar:6.8.0]
    at org.apache.wicket.MarkupContainer$3.component(MarkupContainer.java:961) ~[wicket-core-6.8.0.jar:6.8.0]
    at org.apache.wicket.MarkupContainer$3.component(MarkupContainer.java:957) ~[wicket-core-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:144) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:162) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:162) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:162) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:162) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:123) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.util.visit.Visits.visitChildren(Visits.java:192) ~[wicket-util-6.8.0.jar:6.8.0]
    at org.apache.wicket.MarkupContainer.visitChildren(MarkupContainer.java:875) ~[wicket-core-6.8.0.jar:6.8.0] 

And my NumberField class:

public class NumberField<T extends Number & Comparable<T>> extends SampleField implements HasUnits {

    private static final long serialVersionUID = 4885709350741384398L;

    private NumberInput<T> numberInput;
    private String unit;

    public NumberField(String id, IModel<String> fieldLabel, IModel<T> model) {
        super(id, fieldLabel, model);
    }

    public NumberField(String id, IModel<String> fieldLabel) {
        super(id, fieldLabel);
    }

    public NumberField(String id, IModel<String> fieldLabel, String unit) {
        super(id, fieldLabel);
        this.unit = unit;
    }

    @SuppressWarnings("unchecked")
    public void onInitialize() {
        super.onInitialize();
        Form<T> f = new Form<>("form");
        this.numberInput = new NumberInput<T>("value",
            (IModel<T>) getDefaultModel());
        this.numberInput.setUnit(unit);
        f.add(numberInput);
        add(f);
    }

}

The SampleField class:

public abstract class SampleField extends Panel {

    private static final long serialVersionUID = 1L;

    public SampleField(String id, IModel<String> fieldLabel) {
        super(id);
        addFieldName(fieldLabel);
    }

    public SampleField(String id, IModel<String> fieldLabel, IModel<?> model) {
        super(id, new CompoundPropertyModel<>(model));
        addFieldName(fieldLabel);
    }

    private final void addFieldName(IModel<String> fieldLabel) {
        add(new Label("fieldname", fieldLabel));
    }

}

Does anyone have an idea on how I can solve this?

Was it helpful?

Solution 2

Try with an AjaxEventBehavior("change") on your NumberField. For anything more serious you'll have to add the behavior to the wrapped numberInput.

OTHER TIPS

You should extend SampleField from FormComponent if you want a custom form component that can use AjaxFormComponentUpdatingBehavior. But I'm not sure you really need a custom form component and not just a TextField("size").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top