Table of contents
1.
Introduction
2.
Vaadin Basic Layouts
3.
Vertical Layout
3.1.
Vertical Alignment
3.1.1.
Alignment Positions
3.2.
Horizontal Alignment
3.2.1.
Alignment Positions
4.
Horizontal Layout
4.1.
Vertical Alignment
4.1.1.
Alignment Positions
4.2.
Horizontal Alignment
4.2.1.
Alignment Positions
5.
Spacing
6.
Padding
7.
Margin
8.
Expanding Items
9.
Frequently Asked Questions
9.1.
Is Vaadin a widely used framework?
9.2.
Is Vaadin a frontend or a backend system?
9.3.
What is the purpose of Vaadin?
10.
Conclusion
Last Updated: Mar 27, 2024
Medium

Vaadin - Basic Layouts

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Vaadin Flow (previously Vaadin Framework) is a Java web framework used to create web applications and websites. The programming model of Vaadin Flow enables developers to use Java as the programming language for implementing User Interfaces (UIs) without having to use HTML or JavaScript directly.

vaadin logo

In this article, we will discuss Vaadin basic layouts. We will start by introducing two types of Vaadin basic layouts:vertical and horizontal layouts, and the alignment associated with them. Afterward, we will discuss spacing, padding, margin, and expanding items in our discussion of Vaadin basic layouts. So let’s get started!

Vaadin Basic Layouts

Vaadin's layout consists of two basic components: Vertical Layout and Horizontal Layout. They render their contents vertically and horizontally, as their names imply. Components are displayed in the order in which they are added to either layout.

Vertical Layout

Vaadin's layout consists of two basic components:

  •  Vertical Layout and 
  • Horizontal Layout. 
     

They render their contents vertically and horizontally, as their names imply. Components are displayed in the order in which they are added to either layout.

Vertical Layout components can be aligned both vertically and horizontally.

VerticalLayout layout = new VerticalLayout();
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));
layout.add(new Button("Button 4"));
vertical aligned item

Vertical Alignment

Components can be placed at the top, middle, or bottom. Alternatively, you can position components by specifying how excess space (if any) in a layout is distributed among them.

VerticalLayout layout = new VerticalLayout();
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

...

radioGroup.addValueChangeListener(e -> {
    FlexComponent.JustifyContentMode mode = e.getValue().getMode();
    layout.setJustifyContentMode(mode);
});

 

Output:

Start (Default): The position of all the items is at the top.

default aligned item

 

Center: The position of all the items is at the vertical center.

center aligned item

 

End: The position of all the items is at the bottom.

end aligned item

 

Between: The available space is distributed evenly among the items. There is no space before or after the first or last item.

between aligned item

 

Around: The available space is distributed evenly among the items. The space between items is half the size of the space before and after the first item.

around aligned item

 

Evenly: The available space is distributed evenly among the items. The space between items, before the first item, and after the last item is all the same.

evenly aligned item

Alignment Positions

The position of alignment in Vaadin while doing vertical alignment in the vertical layout is given in the following table.

aligned item table

Horizontal Alignment

Vertical Layout components can be left-aligned (the default), centred, right-aligned, or stretched horizontally.

VerticalLayout layout = new VerticalLayout();
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

...

radioGroup.addValueChangeListener(e -> {
    FlexComponent.Alignment alignment = e.getValue().getAlignment();
    layout.setAlignItems(alignment);
});

 

Output:

Start (Default): The position of all the items is set at default, i.e. left aligns/ left to right (LTR) or right aligns/ right to left (RTL) items.

default aligned item

Center: The position of all the items is at the horizontal center.

center aligned item

 

End: The position of all the items is set at the end, i.e. right aligns (LTR) or left aligns (RTL) items.

end aligned item

Stretch: Items with an undefined width are stretched horizontally.

stretch aligned item

 

Alignment Positions

The position of alignment in Vaadin while doing horizontal alignment in the vertical layout is given in the following table.

aligned item table

Horizontal Layout

The horizontal Layout arranges components in a row side by side. It has an undefined width and height by default, which means that its size is determined by the components it contains.

A horizontal Layout, like Vertical Layout, allows for vertical and horizontal component alignment.

HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));
layout.add(new Button("Button 4"));

 

Output:

aligned item

Vertical Alignment

Components can be placed at the top, middle, or bottom. Items can also be stretched vertically along the layout's text baseline.

HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(new TextArea("Text area 1"));
layout.add(new TextArea("Text area 2"));
layout.add(new TextArea("Text area 3"));

...

radioGroup.addValueChangeListener(e -> {
    FlexComponent.Alignment alignment = e.getValue().getAlignment();
    layout.setAlignItems(alignment);
});

 

Output:

Stretch (Default): Items are at default positions and with an undefined height they are stretched horizontally.

default aligned item

 

Start: Items are placed at the top of the layout.

start aligned item

 

Center: Items are placed at the vertical center of the layout.

center aligned item

 

End: Items are placed at the bottom of the layout.

end aligned item

 

Baseline: Items should be placed along the layout's (text) baseline.

baseline aligned item

Alignment Positions

The position of alignment in Vaadin while doing vertical alignment in the horizontal layout is given in the following table.

aligned item table

Horizontal Alignment

Horizontal Layout components can be left-aligned, centred, or right-aligned. Alternatively, you can position components by specifying how excess space in a layout is distributed among them.

HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

...

radioGroup.addValueChangeListener(e -> {
    FlexComponent.JustifyContentMode mode = e.getValue().getMode();
    layout.setJustifyContentMode(mode);
});

 

Output:

Start (Default): The position of all the items is set at default, i.e. left aligns/ left to right (LTR) or right aligns/ right to left (RTL) items.

default aligned item

Center: Items are placed at the horizontal centers.

centred aligned item

End: The position of all the items is set at default, i.e. right aligns (LTR) or left aligns (RTL) items.

end aligned item

Between: The available space is distributed evenly among the items. There is no space before or after the first or last item.

between aligned item

Around: The available space is distributed evenly among the items. The space between items is half the size of the space before and after the first item.

around aligned item

Evenly: The available space is distributed evenly among the items. The space between items, before the first item, and after the last item is all the same.

evenly aligned item

Alignment Positions

The position of alignment in Vaadin while doing horizontal alignment in the horizontal layout is given in the following table.

alignment position

Spacing

Spacing is used to make room for components in the same layout. Spacing can aid in the prevention of misclicks and the differentiation of content areas.

VerticalLayout layout = new VerticalLayout();
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Spacing");
radioButtonGroup.setItems(ENABLED_OPTION, DISABLED_OPTION);
radioButtonGroup.setValue(ENABLED_OPTION);
radioButtonGroup.addValueChangeListener(
        e -> layout.setSpacing(ENABLED_OPTION.equals(e.getValue())));

 

Output:

Disabled: The alignment of the item when the spacing is disabled.

disabled button

 

Enabled: The alignment of the item when the spacing is enabled. 

enabled item button

Padding

Padding is the space between the outer border of a layout and its content. Padding can help separate the content of a layout from its surroundings. The padding theme variant is used to add padding.

VerticalLayout layout = new VerticalLayout();
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Padding");
radioButtonGroup.setItems(ENABLED_OPTION, DISABLED_OPTION);
radioButtonGroup.setValue(ENABLED_OPTION);
radioButtonGroup.addValueChangeListener(
        e -> layout.setPadding(ENABLED_OPTION.equals(e.getValue())));

 

Output:

Disabled: The alignment of the item when the padding is disabled.

disabled item

Enabled: The alignment of the item when the padding is enabled.

enabled item button

Margin

To make space around a layout, use margin.

VerticalLayout layout = new VerticalLayout();
layout.setWidth("auto");
layout.setMargin(true);
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new Button("Button 1"));
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Margin");
radioButtonGroup.setItems(ENABLED_OPTION, DISABLED_OPTION);
radioButtonGroup.setValue(ENABLED_OPTION);
radioButtonGroup.addValueChangeListener(
        e -> layout.setMargin(ENABLED_OPTION.equals(e.getValue())));

 

Output:

Disabled: The alignment of the item when the margin is disabled. 

disabled item

 

Enabled: The alignment of the item when the margin is enabled.

enabled item button

Expanding Items

Components can be designed to expand and take up any extra space in a layout. When multiple components expand, they do so relatively to each other. For example, if you have two items with expanding ratios of 2 and 1, the first item will take up twice as much space as the second.

Button button1 = new Button("Button 1");
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(button1);
layout.add(new Button("Button 2"));
layout.add(new Button("Button 3"));

RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Item sizing");
radioButtonGroup.setItems(DEFAULT_SIZE_OPTION, EXPANDED_SIZE_OPTION);
radioButtonGroup.setValue(DEFAULT_SIZE_OPTION);
radioButtonGroup.addValueChangeListener(e -> layout
        .setFlexGrow(DEFAULT_SIZE_OPTION.equals(e.getValue()) ? 0 : 1,
                button1));

 

Output:

Default Size: The position of the items when expanding alignment is set on default. We had selected the item “Button 1”.

default item size on expand

Expand: The position of the items when expanding alignment is turned on. We had selected the item “Button 1”.

expand item

Frequently Asked Questions

Is Vaadin a widely used framework?

Vaadin is an established web framework for creating rich internet applications. Vaadin makes developing web-based GUIs feel like developing a desktop application, which is great, comfortable, and fast.

Is Vaadin a frontend or a backend system?

Vaadin is an open-source platform for developing modern, collaborative Java backend web apps. It combines UI components, frameworks, and tools into a cohesive web development stack.

What is the purpose of Vaadin?

Compared to traditional web technologies, Vaadin provides many more components, layouts, and listeners; thus, it is far preferable to use Vaadin. Vaadin is fully object-oriented due to its Java foundation. A Java developer can easily create a website by understanding Vaadin classes and their applications.

Conclusion

In this article, we have extensively discussed the Vaadin basic layouts, including two types of Vaadin basic layouts, vertical and horizontal layouts. Afterward, we discussed spacing, padding, margin, and expanding items, concluding our discussion of Vaadin basic layouts.

If you think this blog has helped you with Vaadin basic layouts, and if you would like to learn more, check out our articles Vaadin DialogVaadin Date PickerVaadin Badge, and Vaadin Themes. You can also refer to our guided path on the basics of java and many more on our Website.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass