我有一个GWT标签面板,想要在另一个标签中发生某个事件(例如按钮点击)时重新加载一个标签。有没有办法做到这一点? 另一种可能性是在选择该选项卡时执行一些代码(例如,向选项卡添加新元素)。 任何帮助都会非常感激,我已经坚持了一段时间了。
为了使问题更具体,我在下面提供了一些代码。
我的代码在屏幕上组织,有一个主屏幕启动选项卡面板。并且每个选项卡都有单独的屏幕启动。
主屏幕的简化代码:
public class HomeScreen extends Composite{
public HomeScreen() {
TabPanel tabPanel = new TabPanel();
FlowPanel flowpanel;
flowpanel = new FlowPanel();
ProfileTabScreen profileTabScreen = new ProfileTabScreen();
flowpanel.add(profileTabScreen);
tabPanel.add(flowpanel, "Profile");
flowpanel = new FlowPanel();
GroupsTabScreen groupsTabScreen = new GroupsTabScreen();
flowpanel.add(groupsTabScreen);
tabPanel.add(flowpanel, "Groups");
initWidget(tabPanel);
}
}
我要从中启动重新加载的标签页的代码:
private VerticalPanel groupPanel = new VerticalPanel();
private Button newGroupButton = new Button("New group");
public GroupsTabScreen() {
newGroupButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
createNewGroup();
}
});
groupPanel.add(newGroupButton);
initWidget(groupPanel);
}
必须重新加载的标签页的代码:
private VerticalPanel profilePanel = new VerticalPanel();
private Label label = new Label("No groups yet.");
public ProfileTabScreen() {
profilePanel.add(label);
initWidget(profilePanel);
}
所以让我们想象一下,当在groupTab中点击newGroupButton时,我只想更改profileTab中的标签文本(实际上它将是ListBox和其他元素)。
正如我所说,每次选择重新加载整个profileTab也是可以接受的。
答案 0 :(得分:0)
对于按钮单击,您可以使用鼠标或键监听器。将这样的监听器添加到选项卡面板并让它调用一个函数,让我们称之为“rebuild”。
重建功能应该能够访问您要更新的选项卡中的元素并重新计算值。
感谢代码示例。如果主屏幕具有自己的类,则应在初始化之前定义已使用的元素。只是一个简单的例子:
public class HomeScreen extends Composite{
private TabPanel tabPanel;
private FlowPanel profileTabScreenPanel;
private FlowPanel groupsTabScreenPanel;
public HomeScreen() {
tabPanel = new TabPanel();
profileTabScreenPanel = new FlowPanel();
ProfileTabScreen profileTabScreen = new ProfileTabScreen();
profileTabScreenPanel.add(profileTabScreen);
tabPanel.add(flowpanel, "Profile");
groupsTabScreenPanel = new FlowPanel();
GroupsTabScreen groupsTabScreen = new GroupsTabScreen();
groupsTabScreenPanel.add(groupsTabScreen);
tabPanel.add(flowpanel, "Groups");
initWidget(tabPanel);
}
}
优点是您实施的每个处理程序都能够直接访问特定元素,这意味着您可以访问元素或重置元素。
此外,你应该在你的TabsScreen-Elements上添加Getters和setter,这样你就可以获得一个标签。
例如,您可以在createNewGroup() - 函数中执行此操作:
profileTabScreenPanel.getLabel().setText("New Text");
答案 1 :(得分:0)
肮脏的答案是在主屏幕上构建您想要更新的标签并将其传递给两个标签,如下所示:
public class HomeScreen extends Composite {
public HomeScreen() {
TabPanel tabPanel = new TabPanel();
FlowPanel flowpanel;
Label labelToUpdate = new Label("No Groups yet");
flowpanel = new FlowPanel();
ProfileTabScreen profileTabScreen = new ProfileTabScreen(labelToUpdate);
flowpanel.add(profileTabScreen);
tabPanel.add(flowpanel, "Profile");
flowpanel = new FlowPanel();
GroupsTabScreen groupsTabScreen = new GroupsTabScreen(labelToUpdate);
flowpanel.add(groupsTabScreen);
tabPanel.add(flowpanel, "Groups");
initWidget(tabPanel);
}
}
public class GroupsTabScreen {
private VerticalPanel groupPanel = new VerticalPanel();
private Button newGroupButton = new Button("New group");
private final Label labelToUpdate;
public GroupsTabScreen(Label label) {
this.labelToUpdate = label;
newGroupButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
createNewGroup();
labelToUpdate.setText("A group has been created");
}
});
groupPanel.add(newGroupButton);
initWidget(groupPanel);
}
}
public class ProfileTabScreen {
private VerticalPanel profilePanel = new VerticalPanel();
private final Label labelToUpdate;
public ProfileTabScreen(Label label) {
this.labelToUpdate = label;
profilePanel.add(labelToUpdate);
initWidget(profilePanel);
}
}
更漂亮的解决方案是在类之间使用事件总线,以触发和检测事件更改。见How to use the GWT EventBus