Vaadin label gets hidden if other layout component is set to expand - vaadin

If I use following code to create a UI using Vaadin,
#Override
protected void init(VaadinRequest request) {
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.addComponent(new Label("Why am I not shown?"));
Button button = new Button("expanding button");
horizontalLayout.addComponent(button);
horizontalLayout.setExpandRatio(button, 1);
horizontalLayout.setWidth(100, Unit.PERCENTAGE);
setContent(horizontalLayout);
}
The label I added to horizontalLayout is not shown in the created UI. Why is that? What I expect to happen in this case is the label to take its required width and the button to take the rest of the width. But the label is not shown at all.
Please don't ask me why I want to expand the button. This is just a MCVE and my real UI is somewhat more complex.

The Button has undefined size by default. So space will be shared by the 100% sized Label (by default) with expand ratio of 0 and the excess space of the Button cell with expand ratio 1. So all space is given to the excess space of the button.
Set the Label to have a undefined size with label.setSizeUndefined() and will work as you expect.
Note that relative sized components can lost all his space when using a expand ratio.
For example:
HorizontalLayout horizontalLayout = new HorizontalLayout();
Label l1 = new Label("one");
Label l2 = new Label("two");
Label l3 = new Label("three");
horizontalLayout.addComponent(l1);
horizontalLayout.addComponent(l2);
horizontalLayout.addComponent(l3);
horizontalLayout.setExpandRatio(l2, 1);
Will show only label "two".

Related

horizontal layout do not split in horizontal way

I'm trying to display three TextField in a line horizontally, but the components will be displayed in vertical way:
this is the code:
VerticalLayout vertical = new VerticalLayout();
vertical.addComponent(new TextField("Name"));
vertical.addComponent(new TextField("Street address"));
vertical.addComponent(new TextField("Postal code"));
HorizontalLayout horizontal = new HorizontalLayout();
horizontal.addComponent(new TextField("Name"));
horizontal.addComponent(new TextField("Street address"));
horizontal.addComponent(new TextField("Postal code"));
//setWidth("670px");
//setWidth("100%");
addComponents(vertical, horizontal);
when I diplay that I see:
I've tried to increase the width to 100% try to set the width to 900px; But I'm not able to figure out what can be the issue and where to look for?

How adjust the Vaadin components' vertical alignment in a HorizontalLayout?

I want to align the Button on the same line as the Text Fields. The item alignment has been set to Alignment.CENTER, but as you can see from the image, the vertical centers of the TextFields are lower due to the label on top of them.
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.setAlignItems(FlexComponent.Alignment.CENTER);
Div div = new Div(buttonAddPriceRange);
horizontalLayout.add(spanneVon, spanneBis, div);
layout.setAlignItems(Alignment.BASELINE); allows you to align items by the baseline, that is with the bottom.
HorizontalLayout hl = new HorizontalLayout(new TextField("hello"),
new Button("world"));
hl.setAlignItems(Alignment.BASELINE);
add(hl);
Check out the great video Master Vaadin VerticalLayout and HorizontalLayout - layouts in Vaadin Flow to learn more.
Try ...
horizontalLayout.setAlignItems(FlexComponent.Alignment.BASELINE);

Vaadin: TextArea scrolling doesn't work

I have something similar to this code:
TextArea textArea = new TextArea();
textArea.setSizeFull();
Panel dataPanel = new Panel("Panel", textArea);
dataPanel.setSizeFull();
textArea.setValue(... some very long text...);
The problem is that this TextArea appears without vertical scrollbar (and mouse-wheel scrolling also doesn't work), although inner text is longer than TextArea height (I can navigate lower using cursor and keyboard down arrow).
How do I enable scrolling in this component?
A bit weird, but as per the documentation if you disable word-wrapping in a text-area, you'll get the vertical scroll-bar:
Word Wrap
The setWordwrap() sets whether long lines are wrapped ( true - default) when the line length reaches the width of the writing area. If the word wrap is disabled (false), a vertical scrollbar will appear instead. The word wrap is only a visual feature and wrapping a long line does not insert line break characters in the field value; shortening a wrapped line will undo the wrapping.
The following code sample illustrates this behaviour with Vaadin 8.0.6. Please note my class extends Panel to match your sample but at this point you can eliminate it:
public class PanelWithScrollableTextField extends Panel {
public PanelWithScrollableTextField() {
TextArea textArea = new TextArea();
textArea.setWordWrap(false);
textArea.setSizeFull();
setContent(textArea);
setSizeFull();
StringBuffer buffer = new StringBuffer();
IntStream.range(1, 100).forEach(value -> buffer.append(value).append("\r\n"));
textArea.setValue(buffer.toString());
}
}
Result:
P.S. I know it's a bit weird to grasp, but panels are used to scroll surfaces that are larger then the panel size, so if we'd get it working, you'd be scrolling the text area itself, not its content. You can see below a sample to better understand what I mean:
public class PanelWithScrollableTextField extends Panel {
public PanelWithScrollableTextField() {
TextArea textArea = new TextArea();
textArea.setWordWrap(false);
textArea.setHeight("500px"); // fixed size with height larger than the panel
setContent(textArea);
setHeight("100px"); // fixed height smaller than the content so we get a scroll bar
StringBuffer buffer = new StringBuffer();
IntStream.range(1, 100).forEach(value -> buffer.append(value).append("\r\n"));
textArea.setValue(buffer.toString());
}
}
Result:
You can change it CSS also like below .
.v-textarea { overflow-y: auto ! important;}

Vaadin: How do make a button align to the top right of my page?

I need to align a button to the right of my page in a vertical layout.
Please tell me method to do this.
private Button createBackButton() {
Button bButton = new Button("Back");
bButton.setIcon(FontAwesome.ARROW_LEFT);
bButton.addClickListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
doSomething();
}
});
return bButton;
}
Null,
In order to align your button to the top-right of your VerticalLayout, use VerticalLayout's setComponentAlignment() method. Also note that the VerticalLayout itself needs to be big enough so that the button can even have some space to move around in there so it looks like it's being aligned to the top-right. By default the VerticalLayout will just get as big as the components inside it. You need to give it a bigger size using setWidth() and setHeight(), or make it take up the whole space as its parent component/layout using setSizeFull() (note that the parent layout, if any, also needs to be big enough so it has space inside it too).
So the code would look like:
VerticalLayout vl = new VerticalLayout();
vl.setSizeFull();
Button backButton = createBackButton();
vl.addComponent(backButton);
vl.setComponentAlignment(backButton,Alignment.TOP_RIGHT);
Hope that helps.

SmartGWT TabSet height in window

I'm trying to have a TabSet in a Window. I would like the window to fit the contents so I turned autoSize on like I have done with many other windows. The window have a minimum width and height that I want it to be. My problem is that the TabSet in the Window doesn't fill up the height completely.
Here is my setup:
public MyWindow()
{
setHeight(MIN_HEIGHT);
setWidth(MIN_WIDTH);
setAutoSize(true);
theTabSet = new TabSet();
theTabSet.setTabBarPosition(Side.TOP);
theTabSet.setWidth100();
theTabSet.setHeight100();
// I need to set this for it to at least display the contents
theTabSet.setPaneContainerOverflow(Overflow.VISIBL E);
theTabSet.setOverflow(Overflow.VISIBLE);
//This seems to solve my issue buy I think I shouldn't be doing this.
theTabSet.setMinHeight(WINDOW_HEIGHT);
Tab tab1 = new Tab("first");
tab1.setPane(getFirstTab());
Tab tab2 = new Tab("second");
tab2.setTab(getSecondTab());
addItem(theTabSet);
}
private Layout getFirstTab(){
if(theFirstTab == null){
theFirstTab = new VLayout();
theFirstTab.setWidth100();
theFirstTab().setHeight100();
}
return theFirstTab;
}
Please let me know if I am missing something. According to the Layout API:
Like other Canvas subclasses, Layout and Stack components may have %
width and height values. To create a dynamically-resizing layout that
occupies the entire page (or entire parent component), set width and
height to "100%
theTabSet.setMinHeight(WINDOW_HEIGHT);
Seems to be the way to go. The window is set to fit it contents and the layout is set to fill the canvas. The window knows a minimum height and width but initially the layout doesn't know that. So we need to set it manually.

Resources