Creating a Vaadin Flow View With Components

In this chapter, you learn core Vaadin concepts and scaffold the first view of the CRM application.

This chapter covers:

  • Vaadin component basics.

  • Constructing a view with the Vaadin Flow Java API.

A Quick Introduction to Vaadin Flow

Here are the most important Vaadin Flow concepts you need to know to get started.

What Is Vaadin Flow?

Vaadin is a Java framework for building web applications. It has a component-based programming model that allows you to build user interfaces.

Vaadin UI Components

Vaadin includes over 40 UI components to help you build apps faster. By using ready-made building blocks, you are able to focus more of your time on building end-user functionality.

Vaadin components are custom HTML elements that are registered with the browser. They are based on W3C web component standards.

The components have light and dark themes, and you can further customize them through CSS variables to fit your brand.

A grid of Vaadin components

You create a new component by initializing a Java object. For instance, to create a Button, you write:

Creating a Button

  1. Button button = new Button("I'm a button");

Layouts

Layouts determine how components display in the browser window. The most common layout components are HorizontalLayout, VerticalLayout, and Div. The first two set the content orientation as horizontal or vertical, respectively, whereas Div lets you control the positioning with CSS.

You add components to layouts using the add() method.

HorizontalLayout and VerticalLayout provide methods to align items on both the primary and the cross axis. For instance, if you want all components, regardless of their height, to be aligned with the bottom of a HorizontalLayout, you can set the default alignment to Alignment.END:

Setting layout alignment

  1. Button button = new Button("I'm a button");
  2. HorizontalLayout layout = new HorizontalLayout(button, new DatePicker("Pick a date"));
  3. layout.setDefaultVerticalComponentAlignment(Alignment.END);
  4. add(layout);

layout alignment

UI Events

You can add functionality to your application by listening to events, such as click events from buttons, or value-change events from select components.

This example adds the text “Clicked!” to the layout when the button is clicked.

Listening to click events

  1. button.addClickListener(clickEvent ->
  2. add(new Text("Clicked!")));

Where’s the HTML?

One unique Vaadin Flow feature is that you can build web applications entirely in Java. This higher level of abstraction makes development more productive and debugging easier.

Vaadin also supports HTML templates and customizing the code that runs in the browser; you don’t usually need to worry about this.

The Contact List View

The first view is the Contact list view. This view lists all the contacts in the system. Users can search, add, edit, and delete contacts in this view.

You initially focus only on the list view. You add the layout containing the header and sidebar later in the “Navigation and parent layouts” chapter.

In this and the next chapter, you create the required layouts and components for the view. Then, in the chapter that follows, you create a service class for accessing the backend and populating the view with data.

The contact list view consists of a Grid

Locate the ListView.java class under src/main/java. Replace the contents of the file with the following:

ListView.java

  1. package com.example.application.views.list;
  2. import com.example.application.data.entity.Contact;
  3. import com.vaadin.flow.component.button.Button;
  4. import com.vaadin.flow.component.grid.Grid;
  5. import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
  6. import com.vaadin.flow.component.orderedlayout.VerticalLayout;
  7. import com.vaadin.flow.component.textfield.TextField;
  8. import com.vaadin.flow.data.value.ValueChangeMode;
  9. import com.vaadin.flow.router.PageTitle;
  10. import com.vaadin.flow.router.Route;
  11. import com.vaadin.flow.theme.Theme;
  12. @Route(value = "")
  13. @Theme(themeFolder = "flowcrmtutorial")
  14. @PageTitle("Contacts | Vaadin CRM")
  15. public class ListView extends VerticalLayout { 1
  16. Grid<Contact> grid = new Grid<>(Contact.class); 2
  17. TextField filterText = new TextField();
  18. public ListView() {
  19. addClassName("list-view");
  20. setSizeFull();
  21. configureGrid(); 3
  22. add(getToolbar(), grid); 4
  23. }
  24. private void configureGrid() {
  25. grid.addClassNames("contact-grid");
  26. grid.setSizeFull();
  27. grid.setColumns("firstName", "lastName", "email"); 5
  28. grid.addColumn(contact -> contact.getStatus().getName()).setHeader("Status"); 6
  29. grid.addColumn(contact -> contact.getCompany().getName()).setHeader("Company");
  30. grid.getColumns().forEach(col -> col.setAutoWidth(true)); 7
  31. }
  32. private HorizontalLayout getToolbar() {
  33. filterText.setPlaceholder("Filter by name...");
  34. filterText.setClearButtonVisible(true);
  35. filterText.setValueChangeMode(ValueChangeMode.LAZY); 8
  36. Button addContactButton = new Button("Add contact");
  37. HorizontalLayout toolbar = new HorizontalLayout(filterText, addContactButton); 9
  38. toolbar.addClassName("toolbar");
  39. return toolbar;
  40. }
  41. }
  1. The view extends VerticalLayout, which places all child components vertically.

  2. The Grid component is typed with Contact.

  3. The grid configuration is extracted to a separate method to keep the constructor easier to read.

  4. Add the toolbar and grid to the VerticalLayout.

  5. Define which properties of Contact the grid should show.

  6. Define custom columns for nested objects.

  7. Configure the columns to automatically adjust their size to fit their contents.

  8. Configure the search field to fire value-change events only when the user stops typing. This way you avoid unnecessary database calls.

  9. The toolbar uses a HorizontalLayout to place the TextField and Button next to each other.

If your application is still running from the previous step, you only need to perform a build, either with the CMD/Ctrl-F9 keyboard shortcut, or by pressing the “hammer” icon in the toolbar. Vaadin will automatically reload your browser to display the changes.

The build project button is in the IntelliJ toolbar

You can keep the server running throughout the tutorial. There are only a couple of cases where you need to restart the server; these will be highlighted in the instructions.

You should now see the empty view structure in the browser window. In the next chapter, you will build a component for the form that is used for editing contacts.

The completed list view has all the components

migration assistance

Download free e-book.
The complete guide is also available in an easy-to-follow PDF format.

https://pages.vaadin.com/en/build-a-modern-web-app-with-spring-boot-vaadin-pdf

Open in anew tab

Show code

render-banner.ts

  1. export class RenderBanner extends HTMLElement {
  2. connectedCallback() {
  3. this.renderBanner();
  4. }
  5. renderBanner() {
  6. let bannerWrapper = document.getElementById('tocBanner');
  7. if (bannerWrapper) {
  8. return;
  9. }
  10. let tocEl = document.getElementById('toc');
  11. // Add an empty ToC div in case page doesn't have one.
  12. if (!tocEl) {
  13. const pageTitle = document.querySelector(
  14. 'main > article > header[class^=PageHeader-module--pageHeader]'
  15. );
  16. tocEl = document.createElement('div');
  17. tocEl.classList.add('toc');
  18. pageTitle?.insertAdjacentElement('afterend', tocEl);
  19. }
  20. // Prepare banner container
  21. bannerWrapper = document.createElement('div');
  22. bannerWrapper.id = 'tocBanner';
  23. tocEl?.appendChild(bannerWrapper);
  24. // Banner elements
  25. const text = document.querySelector('.toc-banner-source-text')?.innerHTML;
  26. const link = document.querySelector('.toc-banner-source-link')?.textContent;
  27. const bannerHtml = `<div class='toc-banner'>
  28. <a href='${link}'>
  29. <div class="toc-banner--img"></div>
  30. <div class='toc-banner--content'>${text}</div>
  31. </a>
  32. </div>`;
  33. bannerWrapper.innerHTML = bannerHtml;
  34. // Add banner image
  35. const imgSource = document.querySelector('.toc-banner-source .image');
  36. const imgTarget = bannerWrapper.querySelector('.toc-banner--img');
  37. if (imgSource && imgTarget) {
  38. imgTarget.appendChild(imgSource);
  39. }
  40. }
  41. }