Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can override <a href="http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/AbstractSingleSelectChoice.html#getNullKey%28%29" rel="nofollow"><code>AbstractSingleSelectChoice.getNullKey()</code></a> and <a href="http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/AbstractSingleSelectChoice.html#getNullValidKey%28%29" rel="nofollow"><code>AbstractSingleSelectChoice.getNullValidKey()</code></a> 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: <a href="https://issues.apache.org/jira/browse/WICKET-2531" rel="nofollow">Open DropDownChoice null value internationalization key</a></p> <p>For instance: </p> <pre><code>DropDownChoice ddc = new DropDownChoice(id, model, choices){ @Override protected String getNullKey(){ return "customDdcNullValue"; } @Override protected String getNullValidKey(){ return "customDdcNullValue"; } } </code></pre> <p>Will retrieve the <code>&lt;option&gt;</code>'s text from the <code>customDdcNullValue</code> property. </p> <p>These methods seem to have been added in version 1.4.4, if you're on 1.3, you could always use an <code>IChoiceRenderer</code> with the <code>DropDownChoice</code> that returns the proper String in the case of <code>null</code> value. Note that you could retrieve it from resources (with <code>getString()</code>, or <code>StringResourceModel</code>), or grab the value from database/cache if necessary. This may depend on how are you already localizing the choices for non-null values.</p> <p>Also, you could use the source code of <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.apache.wicket/wicket/1.4.8/org/apache/wicket/markup/html/form/AbstractSingleSelectChoice.java#AbstractSingleSelectChoice.getDefaultChoice%28java.lang.Object%29" rel="nofollow"><code>AbstractSingleSelectChoice.getDefaultChoice()</code></a> to roll your own <code>DropDownChoice</code> with this behavior.</p> <p><strong>UPDATE</strong>: Another way of handling this is to provide your own custom value that represents <code>null</code>, or no-choice. Just remember to use <code>setNullValid(false)</code> on the <code>DropDownChoice</code> to prevent <code>null</code> from 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 provide <code>null</code> in the <code>getDefaultChoice()</code> call. </p> <p>When defining the value for your no-selection option, pay special attention to this part of <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.apache.wicket/wicket/1.4.8/org/apache/wicket/markup/html/form/AbstractSingleSelectChoice.java#301" rel="nofollow"><code>AbstractSingleSelectChoice.getDefaultChoice()</code></a>: </p> <pre><code>// Null is not valid. Is it selected anyway? if ((selected == null) || getNoSelectionValue().equals(selected) || selected.equals(EMPTY_STRING)) { </code></pre> <p>Take into account that <code>getNoSelectionValue()</code> returns <code>-1</code>, so please do not use <code>-1</code>, or an empty string, as your custom null value. I learnt this the hard way, believe me, it's not pleasing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload