2

我正在尝试制作 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));
    }

}

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

4

2 回答 2

4

如果您想要一个可以使用 AjaxFormComponentUpdatingBehavior 的自定义表单组件,您应该从 FormComponent 扩展 SampleField。但我不确定您是否真的需要自定义表单组件而不仅仅是 TextField("size")。

于 2014-05-05T16:13:03.573 回答
1

尝试在 NumberField 上使用 AjaxEventBehavior("change")。对于更严重的事情,您必须将行为添加到包装的 numberInput。

于 2014-05-05T19:34:17.137 回答