What does setContent() actually do?

I have the following code;

mainWindow = new Window(“Application name”);

    VerticalLayout baseWindowLayout = new VerticalLayout();

I can not see any content if I add more content to the baseWindowLayout… For example if I add a Label to the layout…

What is the difference between adding the baseWindowLayout layout as a component or as content to the MainWindow? (addComponent(baseWindowLayout) vs. setContent(baseWindowLayout))

If I add the baseWindowLayout I can see the components added to the baseWindowLayout layout…

What is the general approach?

If I set the baseWindowLayout as the default content for the window is it better to add the content to the mainWindow or to the baseWindowLayout??

I know the default layout for a window is a vertical layout, but I don’t know how to set such things as “setComponentAlignment” with a default layout so I always add a new layout as default layout and then adding components to that layout instead…

Please inform me :slight_smile:

The addComponent() for Window adds a component to the root layout of the window, while setContent() sets the actual root layout (or ComponentContainer to be exact). There’s no difference between window.addComponent(c) and window.getContent().addComponent(c).

As the default layout for windows is VerticalLayout, the following codes do the same:

VerticalLayout v = new VerticalLayout();

and by casting the default layout:

VerticalLayout v = (VerticalLayout) window.getContent();

You usually save a couple lines by casting.

For just setting the size:


Window is an extension of Panel. Panels are not component container which means that a bunch of components can not be added directly into the panel. Instead the panel has only one component container inside it which in turn may contain as many components as you like. As you’ve noticed, panel has however a addComponent, but all it does internally is send the component to the “content” of it. In other words panel.addComponent(component) calls panel.getContent().addComponent();

The panel always creates a default VerticalLayout in the constructor which is uses as the content before the developer specifies one to it. Doing mainWindow.setContent(baseWindowLayout); will replace the layout while mainWindow.addComponent(baseWindowLayout); will keep the default layout and add your layout inside of it. Your stack of components looks like this


  VerticalLayout (your own layout)

  VerticalLayout (the default layout)
    VerticalLayout (your own layout)

As you can see, by using addComponent to put your layout into the panel, you’ll get an extra layout in the stack, which is not good. It can interfere in making the view look the way you want it and it will only slow down the rendering.

Your label should show up correctly in either case. I suppose you did something like mainWindow or baseWindowLayout.addComponen(new Label(“hello”)).

Note that in your code getMainWindow().setSizeFull() and mainWindow.setSizeFull() does exactly the same thing so you don’t have to call it twice. Also, if you create your base layout before the window, you can give in the layout directly so it doesn’t have to create the default and then override with the getContent. You can give it in the constructor like this: new Window(“Application name”, baseWindowLayout);

Hope that helps!

Come to think of it, a way to code this so that nothing is displayed is to first call addComponent on the window and first afterwards do setContent. If you first do addComponent, the components will go to the default layout, when you then do setConent() you will remove the default layout and all the components in it and replace it with your own empty layout.