我正在尝试创建一个RadioField,例如(是,否,未知)。对于“是”,我有一个文本字段。我希望如果在文本字段上键入任何内容,那么将在单选按钮上选择“是”。
我试过了,但出现异常:

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);


这是我得到的例外:

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]


还有我的NumberField类:

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);
    }

}


SampleField类:

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));
    }

}


有谁知道如何解决这个问题?

最佳答案

尝试在NumberField上使用AjaxEventBehavior(“ change”)。对于更严重的问题,您必须将行为添加到包装的numberInput中。

07-26 07:14