关于GWT和MVP的TabLayoutPanel

时间:2012-05-15 08:17:00

标签: gwt mvp gwt-tablayoutpanel

我根据这个模型开始使用MVP架构开发GWT接口:

  • 实例化TabLayoutPanel的第一个视图,定义第一个Tab的小部件和一个空的第二个Tab。
  • on选择第二个选项卡我触发一个事件,该事件将整个TabLayoutPanel发送到第二个视图,该视图将定义第二个Tab的小部件。

在第二个视图中,我收到了相应的TabLayoutPanel,但是当我检索第二个Tab时,进行更改并将其插入旧面板中,我收到消息“此小部件的父级未实现HasWidgets”,第二个Tab消失。

感谢您帮我看看这里的真正问题是什么,或者如何做到这一点。

我添加了带注释的第二个视图代码。

public class MDP2View extends Composite implements MDP2Presenter.Display {

    private final TabLayoutPanel tabPanel;
    private final VerticalPanel MDP2;
    private final Label label;

    public MDP2View(HasSelectionHandlers<Integer> tabPanel) {
            // Getting the TabLayoutPanel created on the first View  
        this.tabPanel = (TabLayoutPanel) tabPanel;
            // Getting the second Tab (this will remove the Tab from the TabLayoutPanel)
        MDP2 = (VerticalPanel) this.tabPanel.getWidget(1); 
        initWidget(MDP2);
            // Adding a label to the Tab
        label = new Label();
        label.setText("onSelectionHandler Works!!!");
        MDP2.add(label);
            // Inserting the Tab in the tabPanel
        this.tabPanel.insert(MDP2, "MDP2", 1);
}

2 个答案:

答案 0 :(得分:0)

当您将其构造函数中的窗口小部件添加到父窗口时,问题就开始了。这导致您的代码中出现此错误。首先,通过调用MDP2initWidget窗口小部件附加到MDP2View的新父窗口。 MDP2ViewMDP2的新父级(因为它现在已从tabPanel中删除)。然后在插入中你可能打算插入你的视图,而是将它唯一的子元素插入小部件MDP2。抛出错误是因为insert将隐式尝试通过方法MDP2从其父MDP2View中删除removeParent,并且不允许MDP2View的子窗口小部件。如果我是正确的,这应该使它工作this.tabPanel.insert(this, "MDP2", 1);

但是从这个构造函数中移除与tabPanel的交互并将其移动到例如selectionHandler的实现更好。这将使您的视图更清洁。这段代码只是等待未来的错误:强制转换为TabLayoutPanel,强制转换为VerticalPanel;如果更改该窗口小部件会导致运行时错误。获取并插入硬编码位置1;如果你在1之前添加一个标签怎么办你的代码表现不正确。

答案 1 :(得分:0)

我认为您必须创建MDP2对象而不在现有窗口小部件上获取引用(此处为this.tabPanel.getWidget(1))。然后,将全新的MDP2插入tabpanel#1

类似的东西:

MDP2 = initWidget(new VerticalPanel()); 
// Adding a label to the Tab
label = new Label();
label.setText("onSelectionHandler Works!!!");
MDP2.add(label);
// Inserting the Tab in the tabPanel
this.tabPanel.insert(MDP2, "MDP2", 1);

我认为在已经存在的东西上调用方法“init”可能很危险:)

相关问题