It is better to extend DropDownChoice with your predefined parameters, and not a Panel with real DropDownChoice.
There are at least two advantages of this approach:
- You don't need to create separate markup file, as it comes with
Panel-approach.
- You could use
DropDownChoice methods directly. Otherwise, you should forward such methods as Panel's methods, or implement getter method for DDC.
So, it would be better to something like this:
public class CityDropDownChoice extends DropDownChoice<City> // use your own generic
{
/* define only one constructor, but you can implement another */
public CityDropDownChoice ( final String id )
{
super ( id );
init();
}
/* private method to construct ddc according to your will */
private void init ()
{
setChoices ( /* Your cities model or list of cities, fetched from somewhere */ );
setChoiceRenderer ( /* You can define default choice renderer */ );
setOutputMarkupId ( true );
/* maybe add some Ajax behaviors */
add(new AjaxEventBehavior ("change")
{
@Override
protected void onEvent ( final AjaxRequestTarget target )
{
onChange ( target );
}
});
}
/*in some forms you can override this method to do something
when choice is changed*/
protected void onChange ( final AjaxRequestTarget target )
{
// override to do something.
}
}
And in your forms simply use:
Form form = ...;
form.add ( new CityDropDownChoice ( "cities" ) );
Think that this approach will suit your needs.