20

When I first load a page, the default option on a dropdownchoice is "Choose One". Is there a way to keep it in the dropdown, even when I have selected a choice?
(In case I would like to put nothing later)

2 Answers 2

40

You need to use the DropDownChoice.setNullValid() method. From the javadoc:

Determines whether or not the null value should be included in the list of choices when the field's model value is nonnull, and whether or not the null_valid string property (e.g. "Choose One") should be displayed until a nonnull value is selected. If set to false, then "Choose One" will be displayed when the value is null. After a value is selected, and that change is propagated to the underlying model, the user will no longer see the "Choose One" option, and there will be no way to reselect null as the value. If set to true, the null string property (the empty string, by default) will always be displayed as an option, whether or not a nonnull value has ever been selected. Note that this setting has no effect on validation; in order to guarantee that a value will be specified on form validation, FormComponent.setRequired(boolean). This is because even if setNullValid() is called with false, the user can fail to provide a value simply by never activating (i.e. clicking on) the component.

If you want to keep the "Choose One" text with NullValid = true you can use a line similar to the following one in your Application.properties file:

nullValid=[Choose one]
2
  • 1
    Nice, seems this method slipped my attention. Thx !
    – Sephy
    Aug 9, 2011 at 15:07
  • 6
    +1 It doesn't have to be the application properties though, in fact usually it's your page properties, like "Choose a thingymajig". And you can prefix it with your component id, to define specific messages for different components on the same page.
    – biziclop
    Aug 10, 2011 at 10:00
5

In .java:

DropDownChoice<Boolean> myDropDown = new DropDownChoice<>(
    "myDropDownWicketId", model, Arrays.asList(true, false), renderer);
myDropDown.setNullValid(true);

In .properties file associated with java class add:

myDropDownWicketId.nullValid=Choose One
myDropDownWicketId.true=Yes
myDropDownWicketId.false=No

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.