Problem with alignment in united cells in GridLayout

Hello,

I have a problem with alignment of a Label which takes two cells horizontally in GridLayout. My GridLayout has 2x2 cells. The label takes the two cells in the first row.
I want my label to be in the middle, right above the border between the two cells below it. I set it with layout.setComponentAlignment(label, Alignment.MIDDLE_CENTER), but the label stays at the left side of the window.
Please help.

Hi,

You most likely encountered one of the most common gotchas in Vaadin: the Label component is by default 100% wide, hence it taking the width of the both cells and aligning to the left.

myLabel.setSizeUndefined() should cure the issue.

Thank you very much Jouni,
that resolved my problem :slight_smile:
Now alignment works fine.

This little “feature” of Label+GridLayout is such a pain that I add this short static helper method to my Vaadin projects:

   static public Label makeLabelSizeUndefined ( String caption )
    {
        Label label = new Label( caption );
        label.setSizeUndefined();
        return label;
    }

So I call like this:

Label phoneLabel = WidgetHelper.makeLabelSizeUndefined( "Phone Number : " );