我想创建自定义Window使用静态工厂样式(或单例模式)。
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);
}
}
并调用 MyWindow.getInstance().show();
第一次调用没问题,但关闭此窗口并重新打开后,我在控制台上收到以下错误日志。
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)
最佳答案
我认为最简单的方法是每次调用 show() 方法时创建一个新的 Window 对象。
关于vaadin - 在 Vaadin 中将组件创建为静态工厂样式(单例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881151/