Dennis S
Dennis S

Reputation: 858

Vaadin: Sub window

I'm trying to use a sub window but either the components I try to put in it show up in the "mainwindow" or I receive a java.lang.UnsupportedOperationException. I'll show you both cases. I'd like to place a HorizontalLayout in the sub window when I need to put in some real components and not just a Label and a Button.

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);
    } 
}

When I run this and open a new sub window using

window = new Window("Title");
setMainWindow(window);
window.addComponent(new SubWindow(window));

I get

SEVERE: Terminal error:

com.vaadin.event.ListenerMethod$MethodException

Cause: java.lang.UnsupportedOperationException at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510) at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)

...

Caused by: java.lang.UnsupportedOperationException at com.vaadin.ui.CustomComponent.removeComponent(CustomComponent.java:248) at com.vaadin.ui.AbstractComponentContainer.addComponent(AbstractComponentContainer.java:207)

...

On the other hand, if I switch place between setCompositionRoot(layout) and myWindow.addComponent(layout), the Label and the Button just end up in the main window instead of the newly created sub window.

What am I missing?

Upvotes: 1

Views: 5561

Answers (1)

Jens Jansson
Jens Jansson

Reputation: 4686

i suggest that you extend Window directly than go through a CustomLayout. A layout can't contain a window - it goes the other way around.

Change

  • public class SubWindow extends CustomComponent to public class SubWindow extends Window
  • myWindow = new Window("My Dialog"); to setCaption("My Dialog");
  • and

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

    to setContent(layout);

That is the standard way of creating a subwindow, exactly the same way you create the main window. I would also move the mainWindow.addWindow(myWindow); outside of the class and not pass the mainwindow object to the subwindow, because that is not really the part of the subwindow object.

Upvotes: 3

Related Questions