Hey John, Thanks for your advice, the example you gave me should handle
what I need to do.

-Clay

-----Original Message-----
From: John Krasnay [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 20, 2007 12:15 PM
To: users@wicket.apache.org
Subject: Re: DropDownChoice Question

On Thu, Sep 20, 2007 at 11:33:31AM -0400, Clay Lehman wrote:
> And I want to be able to add a DropDownChoice to my form that is
> something like this and have it automatically update Item.type in my
> model:
> 
>  
> 
>  public class EditItemForm extends Form{         
> 
>        
> 
>             public EditItemForm(final String id, final Item item){
> 
> 
>                 super(id, new CompoundPropertyModel(item));
> 
>  
> 
>                 List choices = new ArrayList(); 
> 
>                 choices.add(new Options("1001","Choice1"));
> 
>                 choices.add(new Options("2004","Choice4"));
> 
>                 choices.add(new Options("305","Choice5"));
> 
>                 
> 
>                 final DropDownChoice ddc = 
> 
>                     new DropDownChoice("ddc",new
> PropertyModel(theItem,"type"),choices,new
ChoiceRenderer("display","id")
> );                     
> 
>                 add(ddc);
> 
>             }
> 
>  
> 
>             public final void onSubmit(){
> 
>                 getRequestCycle().setRedirect(false);       
> 
>                 System.out.println("onSubmit() - theItem.type:
> "+theItem.type);
> 
>             }
> 
>             protected void validate(){
> 
>                 super.validate();
> 
>                 getRequestCycle().setRedirect(false);                
> 
>             }
> 
>         }
> 

Hi Clay,

The trick with DropDownChoice that most people seem to miss is that the
objects that you put in the choices model must be of the same type as
the property you're setting. Since your property is a setting is a
String, you want to make your choices a list of the possible Strings to
which to set it. Try something like this...

    final Map<String, String> choiceMap = new HashMap<String, String>();
    choiceMap.put("1001", "Choice 1");
    choiceMap.put("2004", "Choice 4");
    choiceMap.put("305", "Choice 5");
    
    List<String> choices = new ArrayList<String>();
    choices.add("1001");
    choices.add("2004");
    choices.add("305");
        
    DropDownChoice ddc = new DropDownChoice(
            "ddc", 
            new PropertyModel(theItem, "type"), 
            choices,
            new IChoiceRenderer() {
                public Object getDisplayValue(Object object) {
                    return choiceMap.get(object.toString());
                }
                public String getIdValue(Object object, int index) {
                    return object.toString();
                }
            });

jk

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to