I have the following piece of code that I wrote. I am trying to center both the texts one after another in the panel like this:
Your name is:
Can anyone tell me how to go about doing this?
VerticalLayout layout1 = new VerticalLayout();
Label label = new Label("Your name is: ");
Label label1 = new Label(name);
Best How To :
Labels in Vaadin have a default width of 100%, meaning the
Label component fills the entire horizontal space, leaving no room for alignment. The reason for this default is that in most cases you'll want the text to wrap once it reaches the edge of the parent component.
To center the labels in your code, just set their widths to undefined by doing
The reson for the extra vertical space is in the
VerticalLayout. Since you've set an expicit height for it but haven't specified any expand ratios for the slots, it's making both of the slots equally high; half the height of the layout each. To get around that, you can do one of two things: get rid of the height in
layout1 by replacing the call to
layout1.setWidth("100%");—or set an expand ratio for the second slot by doing
layout1.setExpandRatio(label1, 1);. Setting the expand ratio will cause the layout to use all the extra space in the second slot, leaving the two labels closer together.
setExpandRatio you'll of course also have to change the alignment of
TOP_CENTER, so that the empty space is placed below the label and not evenly around it.