I need to set the null value string for a dropDownChoice component. The default value for null choice is “Choose One”, which is hardcoded in the Wicket AbstractSingleChoice class.
The usual way of overriding this is to define a property resource.
However my DropdownChoice component is dynamically generated at runtime, and added to a panel (which is within a datatable). The DropDownChoice is not specifically referenced in the markup.
I’m looking for suggestions on how to programmatically override the default null value string in this situation. It would be nice if the DropDownChoice constructor had an additional parameter for this.
Do I need to programmatically create a new property resource?
Hope this makes sense.
You can override
AbstractSingleSelectChoice.getNullKey()andAbstractSingleSelectChoice.getNullValidKey()in order to make Wicket retrieve a localized resource from a .properties or .xml file that does not have the component id as part of the key. This is documented in this JIRA issue: Open DropDownChoice null value internationalization keyFor instance:
Will retrieve the
<option>‘s text from thecustomDdcNullValueproperty.These methods seem to have been added in version 1.4.4, if you’re on 1.3, you could always use an
IChoiceRendererwith theDropDownChoicethat returns the proper String in the case ofnullvalue. Note that you could retrieve it from resources (withgetString(), orStringResourceModel), or grab the value from database/cache if necessary. This may depend on how are you already localizing the choices for non-null values.Also, you could use the source code of
AbstractSingleSelectChoice.getDefaultChoice()to roll your ownDropDownChoicewith this behavior.UPDATE: Another way of handling this is to provide your own custom value that represents
null, or no-choice. Just remember to usesetNullValid(false)on theDropDownChoiceto preventnullfrom appearing when there is a selected value, and initialize the ddc’s model with your no-selection value when you still don’t know its value in order to avoid Wicket providenullin thegetDefaultChoice()call.When defining the value for your no-selection option, pay special attention to this part of
AbstractSingleSelectChoice.getDefaultChoice():Take into account that
getNoSelectionValue()returns-1, so please do not use-1, or an empty string, as your custom null value. I learnt this the hard way, believe me, it’s not pleasing.