在Wicket TextField上添加侦听器

时间:2014-05-05 15:42:53

标签: java wicket textfield wicketstuff

我正在尝试创建一个RadioField,比如(是的,不,未知)。对于“是”,我有一个文本字段。我希望如果在文本字段上输入任何内容,将在radiobutton上选择“是”。 我试过这个,但我得到了一个例外:

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

}

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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

尝试在NumberField上使用AjaxEventBehavior(&#34;更改&#34;)。对于任何更严重的事情,您必须将行为添加到包装的numberInput。