DropDownChoice设置选择的特定索引

时间:2011-05-02 14:01:31

标签: java drop-down-menu wicket

我有一个DropDownChoice:

       DropDownChoice timePeriod = new DropDownChoice("timePeriod", Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {

            private static final long serialVersionUID = 10102L;

            @Override
            public String getIdValue(Object object, int index) {
                return ((TimePeriod) object).getId() + "";
            }

            @Override
            public Object getDisplayValue(Object object) {
                return ((TimePeriod) object).getPeriodType();
            }
        });
        timePeriod.setNullValid(false);

我的问题是:

  1. 如何将所选索引设置为2或任何其他索引?
  2. 如何删除第一个默认空白选项?
  3. 谢谢。

3 个答案:

答案 0 :(得分:5)

您应该能够使用PropertyModel设置所选索引,而不是对下拉列表的值进行硬编码。我现在无法测试,但它会像

String timePeriodValue = "Bi-Weekly";

DropDownChoice timePeriod = new DropDownChoice("timePeriod",
  new PropertyModel(this, "timePeriodValue"),
  Arrays.asList(/* your options */),
  new IChoiceRenderer() {
    // ...
// Again, this is pseudocode only

作为奖励,设置默认值的行为应该消除空白选项问题。

答案 1 :(得分:1)

DropDownChoice有一个接受默认值的构造函数。 或者在你的代码中你可以添加timePeriod.setModel(Model.of(new TimePeriod(2,“Bi-Weekly”)))

我猜TimePeriod有适当的#equals()和#hashCode()实现。

关于空白选项:请参阅org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.isNullValid()

答案 2 :(得分:0)

Lord Torgamus和martin-g谢谢你们两位。我做了一个小测试。它运作得很好。正如Lord Torgamus所说,

@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
public class MyPage extends WebPage {

    public MyPage() {       
        add(new MyForm("form"));
    }

    private class MyForm extends Form {

        public MyForm(String id) {
            super(id);
            setOutputMarkupPlaceholderTag(true);
            setModel(new Model(new FormModel()));
            final DropDownChoice timePeriod = new DropDownChoice("timePeriod", new PropertyModel(getModel(), "timePeriod"), Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {

                private static final long serialVersionUID = 10102L;

                @Override
                public String getIdValue(Object object, int index) {
                    return ((TimePeriod) object).getId() + "";
                }

                @Override
                public Object getDisplayValue(Object object) {
                    return ((TimePeriod) object).getPeriodType();
                }
            });
            timePeriod.setNullValid(false);
            add(timePeriod);
            timePeriod.setOutputMarkupPlaceholderTag(true);
            timePeriod.add(new AjaxFormComponentUpdatingBehavior("onChange") {

                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    MyForm form = (MyForm) timePeriod.getParent();
                    FormModel formModel = (FormModel) form.getModelObject();
                    formModel.setTimePeriod(new TimePeriod(4, "Monthly"));
                    form.setModel(new Model(formModel));
                    target.addComponent(form);
                }
            });
        }

        @Override
        public void onSubmit() {
            System.out.println(getModelObject());
        }

    }
    private class FormModel implements Serializable {
        private TimePeriod timePeriod = new TimePeriod(2, "Bi-Weekly");

        public FormModel() {

        }

        public TimePeriod getTimePeriod() {
            return timePeriod;
        }

        public void setTimePeriod(TimePeriod timePeriod) {
            this.timePeriod = timePeriod;
        }

        @Override
        public String toString() {
            return "FormModel [timePeriod=" + timePeriod + "]";
        }

    }

    private class TimePeriod implements Serializable {
        private int id;
        private String periodType;

        public TimePeriod(int id, String periodType) {
            super();
            this.id = id;
            this.periodType = periodType;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getPeriodType() {
            return periodType;
        }

        public void setPeriodType(String periodType) {
            this.periodType = periodType;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + getOuterType().hashCode();
            result = prime * result + id;
            result = prime * result + ((periodType == null) ? 0 : periodType.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            TimePeriod other = (TimePeriod) obj;
            if (!getOuterType().equals(other.getOuterType()))
                return false;
            if (id != other.id)
                return false;
            if (periodType == null) {
                if (other.periodType != null)
                    return false;
            } else if (!periodType.equals(other.periodType))
                return false;
            return true;
        }

        private LoginPage getOuterType() {
            return LoginPage.this;
        }

        @Override
        public String toString() {
            return "TimePeriod [id=" + id + ", periodType=" + periodType + "]";
        }

    }
}

以上代码是为其他用户提供的,因为它可能会有所帮助,我将其编写用于测试目的,因此所有类都写在一个.java文件中,尽管这是不可取的。

谢谢。