【问题标题】:Dynamically adding multiple GWT RadioButton groups动态添加多个 GWT RadioButton 组
【发布时间】:2011-07-31 18:37:43
【问题描述】:
在我的应用程序中,我有一个小部件面板(所有相同类型的小部件),以及允许用户在面板中添加和删除小部件的按钮。每个小部件内部都有一个 GWT RadioButton 组。小部件使用 GWT UiBinder,因此在我的 ui.xml 文件中,我给每个 RadioButton 一个组名,以便它们链接在一起。
但是,当将 2 个或更多此小部件添加到面板时,这会成为一个问题,因为所有小部件中的所有 RadioButtons 都具有相同的组名。我希望每个小部件中的每个 RadioButton 组都独立于其他小部件。我该怎么做?
【问题讨论】:
标签:
gwt
radio-button
uibinder
【解决方案1】:
您可以在您的小部件构造函数中引入一个 groupName 参数,然后使用@UiFactory 来构建 RadioButtons:
private String groupName;
public MyWidget(String groupName) {
this.groupName = groupName;
initWidget(uiBinder.createAndBindUi(this));
}
@UiFactory
RadioButton makeRadioButton() {
return new RadioButton(groupName);
}
将为您在 UiBinder xml 文件中使用的所有<g:RadioButton>s 自动调用makeRadioButton()。
现在您可以动态地创建每个具有不同无线电组名称的小部件。