Lahiru Chandima
Lahiru Chandima

Reputation: 24128

Vaadin label gets hidden if other layout component is set to expand

If I use following code to create a UI using Vaadin,

@Override
protected void init(VaadinRequest request) {
    HorizontalLayout horizontalLayout = new HorizontalLayout();

    horizontalLayout.addComponent(new Label("Why am I not shown?"));
    Button button = new Button("expanding button");
    horizontalLayout.addComponent(button);
    horizontalLayout.setExpandRatio(button, 1);

    horizontalLayout.setWidth(100, Unit.PERCENTAGE);
    setContent(horizontalLayout);
}

The label I added to horizontalLayout is not shown in the created UI. Why is that? What I expect to happen in this case is the label to take its required width and the button to take the rest of the width. But the label is not shown at all.

Please don't ask me why I want to expand the button. This is just a MCVE and my real UI is somewhat more complex.

Upvotes: 1

Views: 1039

Answers (1)

Jose Luis Martin
Jose Luis Martin

Reputation: 10709

The Button has undefined size by default. So space will be shared by the 100% sized Label (by default) with expand ratio of 0 and the excess space of the Button cell with expand ratio 1. So all space is given to the excess space of the button.

Set the Label to have a undefined size with label.setSizeUndefined() and will work as you expect.

Note that relative sized components can lost all his space when using a expand ratio.

For example:

 HorizontalLayout horizontalLayout = new HorizontalLayout();
 Label l1 = new Label("one");
 Label l2 = new Label("two");
 Label l3 = new Label("three");
 horizontalLayout.addComponent(l1);
 horizontalLayout.addComponent(l2);
 horizontalLayout.addComponent(l3);
 horizontalLayout.setExpandRatio(l2, 1);

Will show only label "two".

Upvotes: 7

Related Questions