【问题标题】:Vaadin: Sub windowVaadin:子窗口
【发布时间】:2011-06-21 08:10:00
【问题描述】:

我正在尝试使用子窗口,但我尝试放入其中的组件显示在“主窗口”中,或者我收到 java.lang.UnsupportedOperationException。我将向您展示这两种情况。当我需要放入一些真实的组件而不仅仅是标签和按钮时,我想在子窗口中放置一个 Horizo​​ntalLayout。

public class SubWindow extends CustomComponent {

    Window mainWindow;  // Reference to main window
    Window myWindow;    // The window to be opened

    public SubWindow(Window main) {
        mainWindow = main;
        createWindow();
    }

    public void createWindow() {
        myWindow = new Window("My Dialog");

        HorizontalLayout layout = new HorizontalLayout();

        // Add the window inside the main window. 
        mainWindow.addWindow(myWindow);

        layout.addComponent(new Label("A label"));
        layout.addComponent(new Button("Ok"));

        // The composition root MUST be set
        setCompositionRoot(layout);

        myWindow.addComponent(layout);
    } 
}

当我运行它并使用

打开一个新的子窗口时
window = new Window("Title");
setMainWindow(window);
window.addComponent(new SubWindow(window));

我明白了

严重:终端错误:

com.vaadin.event.ListenerMethod$MethodException

原因:java.lang.UnsupportedOperationException 在 com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510) 在 com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)

...

原因:java.lang.UnsupportedOperationException 在 com.vaadin.ui.CustomComponent.removeComponent(CustomComponent.java:248) 在 com.vaadin.ui.AbstractComponentContainer.addComponent(AbstractComponentContainer.java:207)

...

另一方面,如果我在 setCompositionRoot(layout) 和 myWindow.addComponent(layout) 之间切换位置,Label 和 Button 只会出现在主窗口中,而不是新创建的子窗口中。

我错过了什么?

【问题讨论】:

    标签: vaadin custom-component


    【解决方案1】:

    我建议您直接扩展 Window 而不是通过 CustomLayout。布局不能包含窗口 - 它反过来。

    改变

    • public class SubWindow extends CustomComponentpublic class SubWindow extends Window
    • myWindow = new Window("My Dialog");setCaption("My Dialog");
    • // The composition root MUST be set
      setCompositionRoot(layout);
      
      myWindow.addComponent(layout);
      

      setContent(layout);

    这是创建子窗口的标准方式,与创建主窗口的方式完全相同。我还将mainWindow.addWindow(myWindow); 移到类之外,并且不将主窗口对象传递给子窗口,因为这实际上不是子窗口对象的一部分。

    【讨论】:

    • 感谢您的回复,现在它就像一个魅力。对于有同样问题的人,这里有一个很好的教程Sub windows
    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    相关资源
    最近更新 更多