Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Panel
borderStylesolid
titleTable of contents
Table of Contents
minLevel2

There is no "default" value. The value selected in the dropdown will be the one that is set in the model that the drop down uses. So when you submit, the selected object is pushed into the model, when dropdown renders the selected object is the one that is pulled from the model.

ie:

Code Block
public Page extends WebPage {
   private List letters = Arrays.asList(new String[] { "A", "B", "C" }));
   public String selected = "B";

   public Page() {
     Form form = new Form("form");
     DropDownChoice choice=new DropDownChoice("ddc", new PropertyModel(this, "selected"), letters);\
   }
}

when this page renders, the letter B will be selected because it is the object inside the drop down's model.

-Igor

[Taken from a wicket-user posting - Gwyn 22:57, 9 Apr 2006 (BST)]

Suppose you have an HTML snippet

Code Block
html
html
<select wicket:id="connective">
  <option value="&">AND</option>
  <option value="|">OR</option>
</select>

and your model object has to be either '&' or '|' according to the selected option.

At first, we will create a helper class which will hold the key-value pairs for the options in the DropDownChoice.

Code Block
titleClass representing the key-value pair
public class SelectOption {
  private String key;
  private String value;

  public SelectOption(String key, String value) {
    this.key = key;
    this.value = value;
  }

//...[getters and setters]...
}

Then we create an instance of DropDownChoice with appropriate ChoiceRenderer.

Code Block
SelectOption[] options = new SelectOption[] {new SelectOption("&", "AND"), new SelectOption("|", "OR")};
ChoiceRenderer choiceRenderer = new ChoiceRenderer("value", "key");
add(new DropDownChoice("connective", model, Arrays.asList(options), choiceRenderer));

The ChoiceRenderer will use the value of the 'key' property of SelectOption object in the 'value' attribute in the rendered HTML (e. g. 'key' property value will be used as the option id). The 'value' property will be used as a display value for the given option. Your backing model must use SelectOption as compared to String in the simple example.

Let's rephrase that last bit paraphrasing John Krasnay's recent post to Wicket-Users...

"You give a DDC a model and a list of possible values. The type of object returned by the model and the type of objects in the list must be the same. If your model returns an Integer, so you must pass a list of Integers, not IntegerSelectChoice or anything else.

If you want to display something different than the integer, you have to implement some custom code in ChoiceRenderer.getDisplayValue(). Don't get hung up on the idea that the value returned has to be a property of
the objects in your list. It can be anything, such as the getString("period_" + object.toString()), for example."

~~~

Note

See also the JavaDoc for the ChoiceRenderer. Another example of custom ChoiceRenderer can be found in the DropDownChoice component reference in the Wicket Library.

These are taken from a working app, but cut & pasted, etc, so there may be issues that mean that they won't compile directly - we'll have to see...

Basically, the values in the PhoneModel dropdown depend on the selected PhoneVendor and the examples show two ways of doing the same thing, i.e. updating the PhoneModel DropDown when the PhoneVendor changes.

Code Block
html
html
<form wicket:id="form">
  <select name="PhoneVendor" wicket:id="phoneVendor"></select>
  <select name="PhoneModel" wicket:id="phoneModel"></select>
</form>
Code Block
private DropDownChoice _phoneModelDDC;
   private MyModel _myModel = new MyModel();

   public MyPage(PageParameters parameters) {
        Form form = new Form("form", new CompoundPropertyModel(_myModel));
        add(form);

        addPhoneVendor(form);
        _phoneModelDDC = addPhoneModel(form);
    }

    private void addPhoneVendor(final Form form) {
        ArrayList phoneVendors = new ArrayList(getMySession().getTerminalVendors());
        form.add(new DropDownChoice("phoneVendor", phoneVendors) {
            /**
             * Whether this component's onSelectionChanged event handler should called using
             * javascript if the selection changes.
             *
             * @return True if this component's onSelectionChanged event handler should
             *         called using javascript if the selection changes
             */
            protected boolean wantOnSelectionChangedNotifications() {
                return true;
            }

            /**
             * Called when a option is selected of a dropdown list that wants to be
             * notified of this event.
             *
             * @param newSelection The newly selected object of the backing model
             */
            protected void onSelectionChanged(final Object newSelection) {
                String phoneVendor = (String) newSelection;
                _myModel.setPhoneModel(null);   // Reset the phone model when the vendor changes
                phoneModelDDC.setChoices(getTerminalsByVendor(phoneVendor));
            }
        });
    }

    private DropDownChoice addPhoneModel(Form form) {
        List list = Collections.EMPTY_LIST;
        String phoneVendor = _myModel.getPhoneVendor();
        if (phoneVendor != null) {
            list = getTerminalsByVendor(phoneVendor);
        }

        DropDownChoice phoneModelDDC = new DropDownChoice("phoneModel", list) {
            protected boolean wantOnSelectionChangedNotifications() {
                return true;
            }
        };
        form.add(phoneModelDDC);
        return phoneModelDDC;
    }
Code Block
private DropDownChoice _phoneVendorDDC, _phoneModelDDC;
   private MyModel _myModel = new MyModel();

   public MyPage(PageParameters parameters) {
        Form form = new Form("form", new CompoundPropertyModel(_myModel));
        add(form);

        _phoneVendorDDC = getPhoneVendorDDC(form);
        _phoneModelDDC = getPhoneModelDDC(form);

        form.add(_phoneVendorDDC);
        form.add(_phoneModelDDC);
    }

    private DropDownChoice getPhoneVendorDDC(final Form form) {
        ArrayList phoneVendors = new ArrayList(getMySession().getTerminalVendors());
        DropDownChoice phoneVendorDDC = new DropDownChoice("phoneVendor", phoneVendors);
        // Add Ajax Behaviour...
        phoneVendorDDC.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            protected void onUpdate(AjaxRequestTarget target) {
                // Reset the phone model dropdown when the vendor changes
                _myModel.setPhoneModel(null);
                _phoneModelDDC.setChoices(getTerminalsByVendor(_myModel.getPhoneVendor()));
                target.addComponent(_phoneModelDDC);
            }
        });
        return phoneVendorDDC;
    }

    private DropDownChoice getPhoneModelDDC(Form form) {
        List list = Collections.EMPTY_LIST;
        String phoneVendor = _myModel.getPhoneVendor();
        if (phoneVendor != null) {
            list = getTerminalsByVendor(phoneVendor);
        }
        DropDownChoice phoneModelDDC = new DropDownChoice("phoneModel", list);
        phoneModelDDC.setOutputMarkupId(true);  // Needed for Ajax to update it
        return phoneModelDDC;
    }

When using a dropdown that was initialised to use a nullSelection string (e.g. "Choose One") and onSelectionChanged, when a choice is made the dropdown gets reloaded. As it's no longer got a null selection, the "Choose One" string doesn't appear (which may affect the width of the dropdown if it was previously the longest option string).

If AJAX is used instead, the dropdown doesn't itself get reloaded, so the nullSelection string remains (and can still be selected.)

Another DropDownChoice Example by Adam

Donohoe Digital example - Good example of how to use DropDownChoice. Includes complete code for a key/value select choice and a select choice list that acts as IChoiceRenderer.

Binding Map example - Code sample binding a Map to DropDownChoice to persist an Integer using JPA.

[Cássio Landim post in wordpress 12:00, 5 Feb 2010 (BST)].