You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Clay Lehman <cl...@medfusion.net> on 2007/09/20 17:33:31 UTC

DropDownChoice Question

Hey everyone,

 

I am relatively new to using Wicket, and I have a question about using
DropDownChoice's to put a select box on a form.  

 

I want to display choices which have specific ID values for each option
(instead of indexed choices, like most examples I see), and I want to
update a String variable in the object that I am editing based on the ID
that is selected.  I basically want a DropDownChoice that works like a
TextField, where the value updated in the Model is the ID of the
selected option.

 

I created the following SelectChoice class so that I can create choices,
and render them with new ChoiceRenderer("id","value")...

 

        public class SelectChoice implements Serializable {

            public String id;

            public String display;

            public SelectChoice (String id, String display){

                this.id=id;

                this.display=display;

            }

        }

 

This allows me to properly display the data, but now I get class cast
exceptions, unless the model that I pass in has a SelectOption as its
object.  

 

My main question: 

Is there an easy way to extend/customize the DropDownChoice component,
so that it renders choices from a class like SelectChoice, which has an
id / displayValue pair, but allows me to use a simple PropertyModel as
my model, which updates a String variable with the results of selecting
a choice from the dropdown?

 

For Example I have the an Item class to handle the data: 

 

        private static class Item implements Serializable{

            private String itemid;

            private String name;

            private String description;

            private String type;

            private Collection groups;

            

            public Item(String itemid, String name, String
description,String type,Collection groups){

                this.itemid=itemid;

                this.name=name;

                this.description=description;

                this.type=type;

                this.groups=groups;

            }

        }

 

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);                

            }

        }

 

 

Thanks in advance for any pointers!!

-Clay 

 


RE: DropDownChoice Question

Posted by Clay Lehman <cl...@medfusion.net>.
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:john@krasnay.ca] 
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice Question

Posted by John Krasnay <jo...@krasnay.ca>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org