Creating components as static factory style (Singleton) in Vaadin

依然范特西╮ 提交于 2019-12-11 02:43:37

问题


I would like to create custom Window using static factory style (or with singleton pattern).

public class MyWindow extends CustomComponent {
private static Window window;
private static MyWindow instance;

public static MyWindow getInstance() {
    if (instance == null) {
        instance = new MyWindow();
    }
    return instance;
}

public void show() {
    UI.getCurrent().addWindow(window);
}

private MyWindow() {
    CustomLayout layout = new CustomLayout("My HTML Layout");
    window = new Window("My Window");
    window.center();
    window.setWidth("615px");
    window.setModal(true);
    window.setResizable(false);
    window.setClosable(true);
    window.setContent(layout);
}
}

And call as MyWindow.getInstance().show(); First time calling was ok but after closing this window and while reopened , I got below error logs at my console.

Jul 23, 2014 3:42:39 AM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE: 
java.lang.IllegalStateException: com.vaadin.ui.Window already has a parent.
at com.vaadin.ui.AbstractComponent.setParent(AbstractComponent.java:469)
at com.vaadin.ui.Window.setParent(Window.java:155)
at com.vaadin.ui.UI.attachWindow(UI.java:501)
at com.vaadin.ui.UI.addWindow(UI.java:490)

So , how can I use customize Windows with static factory style and how to hide and show Windows ?


回答1:


I think the easiest way is to create a new Window Object everytime you call the show() method.




回答2:


The error says that your Window already have a parent. It means that it wasn't removed when you closed it. It is actually weird I've never had that error before. But you can try this if you want:

 window.addCloseListener(new CloseListener() {

  @Override
  public void windowClose(CloseEvent e) {
    AbstractSingleComponentContainer.removeFromParent(subwindow);
  }
});

This should resolve your problem.



来源:https://stackoverflow.com/questions/24881151/creating-components-as-static-factory-style-singleton-in-vaadin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!