How do you get values from a dropdown list in Wicket?

10,793

You're looking for the PropertyModel class. Wicket PropertyModels can let you tie the value of a component directly to a value in your source. The Javadoc sample code is

Person person = getSomePerson();
add(new Label("myLabel", new PropertyModel(person, "name"));

When that label is added to the page, it'll display the value in person.name with no extra work on your end.

Your car sample code already uses PropertyModels, so all you have to do is change the target. For example:

car theCar = new car();
final DropDownChoice<String> makes = new DropDownChoice<String>("makes",
        new PropertyModel<String>(theCar, "name"), makeChoices);
final DropDownChoice<String> models = new DropDownChoice<String>("models",
        new PropertyModel<String>(theCar, "model"), modelChoices);

This will set the value of theCar.name to what's in the makes dropdown list and the value of theCar.model to what's in the models dropdown list.

EDIT:
Yes, it's possible to set the values with a button instead of automatically. To do this, don't use the PropertyModels. Instead, create a new Wicket Button object and override its onSubmit() method with code like

theCar.setName(makes.getValue());
theCar.setModel(models.getValue());

Or, if you want to do it AJAXically, put that inside an AjaxFormChoiceComponentUpdatingBehavior's onUpdate() method.

Share:
10,793
ajdar ajdaroğlu
Author by

ajdar ajdaroğlu

Updated on June 04, 2022

Comments

  • ajdar ajdaroğlu
    ajdar ajdaroğlu almost 2 years

    I've found the following Wicket sample code:

    package org.apache.wicket.examples.ajax.builtin;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.wicket.ajax.AjaxRequestTarget;
    import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
    import org.apache.wicket.markup.html.form.DropDownChoice;
    import org.apache.wicket.markup.html.form.Form;
    import org.apache.wicket.model.AbstractReadOnlyModel;
    import org.apache.wicket.model.IModel;
    import org.apache.wicket.model.Model;
    import org.apache.wicket.model.PropertyModel;
    
    /**
     * Linked select boxes example
     * 
     * @author Igor Vaynberg (ivaynberg)
     */
    public class ChoicePage extends BasePage
    {
        private String selectedMake;
    
        private final Map<String, List<String>> modelsMap = new HashMap<String, List<String>>(); // map:company->model
    
        /**
         * @return Currently selected make
         */
        public String getSelectedMake()
        {
            return selectedMake;
        }
    
        /**
         * @param selectedMake
         *            The make that is currently selected
         */
        public void setSelectedMake(String selectedMake)
        {
            this.selectedMake = selectedMake;
        }
    
        /**
         * Constructor.
         */
        public ChoicePage()
        {
            modelsMap.put("AUDI", Arrays.asList(new String[] { "A4", "A6", "TT" }));
            modelsMap.put("CADILLAC", Arrays.asList(new String[] { "CTS", "DTS", "ESCALADE", "SRX",
                    "DEVILLE" }));
            modelsMap.put("FORD", Arrays.asList(new String[] { "CROWN", "ESCAPE", "EXPEDITION",
                    "EXPLORER", "F-150" }));
    
            IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>()
            {
                @Override
                public List<String> getObject()
                {
                    Set<String> keys = modelsMap.keySet();
                    List<String> list = new ArrayList<String>(keys);
                    return list;
                }
    
            };
    
            IModel<List<? extends String>> modelChoices = new AbstractReadOnlyModel<List<? extends String>>()
            {
                @Override
                public List<String> getObject()
                {
                    List<String> models = modelsMap.get(selectedMake);
                    if (models == null)
                    {
                        models = Collections.emptyList();
                    }
                    return models;
                }
    
            };
    
            Form<?> form = new Form("form");
            add(form);
    
            final DropDownChoice<String> makes = new DropDownChoice<String>("makes",
                new PropertyModel<String>(this, "selectedMake"), makeChoices);
    
            final DropDownChoice<String> models = new DropDownChoice<String>("models",
                new Model<String>(), modelChoices);
            models.setOutputMarkupId(true);
    
            form.add(makes);
            form.add(models);
    
            makes.add(new AjaxFormComponentUpdatingBehavior("onchange")
            {
                @Override
                protected void onUpdate(AjaxRequestTarget target)
                {
                    target.addComponent(models);
                }
            });
        }
    }
    

    Suppose I have the following class:

    public class car{
          private String name;
          private String  model;
    
          public setname(String n){
               this.name=n;
          }
          public setModel(String m){
               this.model=m;
          }
          ///  and getters...
    }
    

    I want to create a car object in the sample code, and assign the values selected in the dropdown to the car object. How can I do that?

  • ajdar ajdaroğlu
    ajdar ajdaroğlu over 13 years
    i did what t u said like aCar = new Car("",""); final DropDownChoice<String> makes = new DropDownChoice<String>("makes", new PropertyModel<String>(aCar, "name"), makeChoices); final DropDownChoice<String> models = new DropDownChoice<String>("models", new PropertyModel<String>(aCar,"model"), modelChoices); and it does not work correctly
  • ajdar ajdaroğlu
    ajdar ajdaroğlu over 13 years
    i get the selected car name by coding aCar.setName(this.selectedMake); but i dont know how to assign the model of the car
  • Pops
    Pops over 13 years
    @ajdar, you shouldn't have to assign anything by hand. I'm testing now.
  • ajdar ajdaroğlu
    ajdar ajdaroğlu over 13 years
    ok.Dement made me confused.Can i assign variables to my object using a button and how?
  • ajdar ajdaroğlu
    ajdar ajdaroğlu over 13 years
    when i added AjaxFormChoiceComponentUpdatingBehavior() it gives me the error Behavior can only be added to an instance of a RadioChoice/CheckboxChoice/RadioGroup/CheckGroup.
  • ajdar ajdaroğlu
    ajdar ajdaroğlu over 13 years
    you said " Yes, it's possible to set the values with a button instead of automatically" .I want ajax to set the values automatically and button get the selected vaules.Is this possible?Thanks
  • Pops
    Pops over 13 years
    @ajdar, I don't know, the original version still works for me in test. Did you change the code to reference theCar.name instead of selectedMake? I can post the entire source if you want. I don't understand your most recent comment; what do you mean by "get the selected values"?