Search code examples
javalabelvaadingrid-layoutgridlayoutmanager

Label disappears on GridLayout in Vaadin


When I add a Label to my GridLayout in my Vaadin 7 app, it fails to appear. I pass the Label to the addComponent method. This works in other Layout implementations, but not GridLayout.


Solution

  • Set Width To Undefined

    Adding a Label to a GridLayout is a special and annoying case. Discussed in the forums. Technically a “feature”, not a bug.

    A Label has its width set by default to 100%. Inside a GridLayout, the Label needs to be of "undefined" width.

    This line will fix your problem:

    myLabel.setSizeUndefined();
    

    Helper Method

    I make frequent use of GridLayout to create professional and attractive layouts in Vaadin. This annoyance with Label in a GridLayout drives me batty, so I created this convenience method.

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

    Example usage:

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