最好DropDownChoice
使用您的预定义参数进行扩展,而不是Panel
使用 real进行扩展DropDownChoice
。
这种方法至少有两个优点:
- 您不需要创建单独的标记文件,因为它带有
Panel
-approach。
- 您可以直接使用
DropDownChoice
方法。否则,应转发Panel
's 方法等方法,或为 DDC 实现 getter 方法。
所以,最好是这样的:
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.
}
}
在您的表格中只需使用:
Form form = ...;
form.add ( new CityDropDownChoice ( "cities" ) );
认为这种方法将适合您的需求。