Label wrap text in VerticalLayout


I know this is a common problem but I can’t wrap my head around it.

My class

public class OverView extends Panel implements View {

root = new VerticalLayout();

// Header

header = new HorizontalLayout();

titleLabel = new Label(“Super long text which is wider than the browser window”);
// width is 100% per default

filter = new TextBox(“Search”);


header.setComponentAlignment(filter, Alignment.MIDDLE_RIGHT);
[/code]Now my problem is, that the titleLabel only wraps if the browser width is smaller than the length of the label.
But it should take the filter (searchbar) into consideration when wrapping

This is how I want it:

But I don’t want to set the titleLabel to a fixed size.

Thank you for your help

I think you need to set 100% width both for the titleLabel and filter, and then set expand ratio for the titleLabel (and not for the filter). Perhaps the 100% width for the filter is not necessary, I’m not immediately sure how it went.

Hmm, the filter should have undefined or fixed width, not 100%.

Also, note that the alignment setting does not set text alignment, but alignment of the component within the layout cell. However, as the label has 100% width, it entirely fills the layout cell, and hence the alignment setting has no effect. You can set the text alignment in CSS with the text-align property.

Sorry for the late response.

I still cant seem to figure it out.

The titleLabel will only be wrapped if the browser size is smaller than the whole titleLabel.
I need that the filter will always be shown.

First of all, you need to define sizes for your component containers: set a width for the panel, vertical layout and the horizontal layout.

Once you’ve done this, the only thing you need to change is the expand ratio of the label. Here’s a working version:

public class OverView extends Panel {
        public OverView() {
           VerticalLayout root = new VerticalLayout();

           // Header
            HorizontalLayout header = new HorizontalLayout();

            Label titleLabel = new Label(
                    "Super long text which is wider than the browser window");
            // width is 100% per default

            TextField filter = new TextField("Search");
            header.setComponentAlignment(filter, Alignment.MIDDLE_RIGHT);
            header.setExpandRatio(titleLabel, 1);