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
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.
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() {
setSizeFull();
VerticalLayout root = new VerticalLayout();
setContent(root);
root.setWidth("100%");
// Header
HorizontalLayout header = new HorizontalLayout();
header.setWidth("100%");
Label titleLabel = new Label(
"Super long text which is wider than the browser window");
// width is 100% per default
TextField filter = new TextField("Search");
filter.setWidth("200px");
header.addComponent(titleLabel);
header.addComponent(filter);
header.setComponentAlignment(filter, Alignment.MIDDLE_RIGHT);
header.setExpandRatio(titleLabel, 1);
root.addComponent(header);
}
}