Can you add this to the wiki (http://cwiki.apache.org/WICKET/dropdownchoice-examples.html)? It seems like a commonly asked question :o)
-----Original Message----- From: Erik van Oosten [mailto:[EMAIL PROTECTED] Sent: Friday, March 14, 2008 9:35 AM To: users@wicket.apache.org Subject: Re: How to pre-select a DropDownChoice Funny, I answered the same question to my colleague this morning. Below the code I gave him. What happens is that the model is wrapped in another model. The wrapping model has a defaultModel that is used to return a value when the underlying model returns null. Below the model wrapper you find an example on how to use it. If you find something better, please let us know :) import org.apache.wicket.model.IChainingModel; import org.apache.wicket.model.AbstractWrapModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.IWrapModel; /** * * @author Erik van Oosten */ public class DefaultingModelWrapper implements IWrapModel { private IModel wrappedModel; private IModel defaultModel; public DefaultingModelWrapper(IModel wrappedModel, IModel defaultModel) { this.wrappedModel = wrappedModel; this.defaultModel = defaultModel; } public IModel getWrappedModel() { return wrappedModel; } public Object getObject() { Object value = wrappedModel.getObject(); if (value == null) { value = defaultModel.getObject(); } return value; } public void setObject(Object object) { wrappedModel.setObject(object); } public void detach() { wrappedModel.detach(); defaultModel.detach(); } } public class DefaultingCountryDropDown extends CountryDropDown { public DefaultingCountryDropDown(String id, IModel model) { super(id, new DefaultingModelWrapper(model, new Model("NL"))); } public DefaultingCountryDropDown(String id) { super(id, new DefaultingModelWrapper(getModel(), new Model("NL"))); } } Regards, Erik. Juan Gabriel Arias wrote: > DropDownChoice has a lot of contructors. One of them, > > DropDownChoice(String id, IModel model, IModel choices, ...) > > The first model, could be named "selectedModel". And represents the selected > object, with a wicket model. > > -- Erik van Oosten http://day-to-day-stuff.blogspot.com/ --------------------------------------------------------------------- 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]