Vaadin TextArea tutorial

In Vaadin TextArea tutorial we learn the basics of the Vaadin TextArea component. The Vaadin TextArea example shows the contents of a web page in the TextArea component. The example uses Jsoup library to read the web page contents.

Vaadin

Vaadin is a popular Java web development framework, which is used for building single page web applications.

Vaadin TextArea

TextArea is a multiline text area component that displays plain text.

Vaadin TextArea example

The following program demonstrates the usage of a Vaadin TextArea component. It reads HTML content from a web page, which is specified in the nearby TextField component.

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.9.2</version>
</dependency>

We need to add the jsoup dependency to the Maven POM file. Jsoup is a popular Java HTML Parser.

MyUI.java
package com.zetcode.main;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.zetcode.service.ReadPage;

@Theme("mytheme")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        VerticalLayout layout = new VerticalLayout();

        TextArea area = new TextArea();
        area.setWidth("650");
        area.setHeight("350");
        TextField field = new TextField("Enter an URL");
        field.setWidth("350");

        Button button = new Button("Submit");
        button.addClickListener(e -> {
            String content = ReadPage.read(field.getValue());
            area.setValue(content);
        });

        layout.addComponents(area, field, button);
        layout.setMargin(true);
        layout.setSpacing(true);

        setContent(layout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

In the code example, we have three components: TextArea, TextField, and Button.

VerticalLayout layout = new VerticalLayout();

In our example, we display three components in a column with VerticalLayout.

TextArea area = new TextArea();
area.setWidth("650");
area.setHeight("350");

A TextArea component is created; its with and height is increased.

TextField field = new TextField("Enter an URL");

A TextField component is created; here we provide the name of the web page to be read.

Button button = new Button("Submit");
button.addClickListener(e -> {
    String content = ReadPage.read(field.getValue());
    area.setValue(content);
});

In the button event handler, we read the web page with ReadPage.read() method and set the HTML data to the TextArea with the setValue() method.

layout.addComponents(area, field, button);

The three components are added to the vertical layout with addComponents().

ReadPage.java
package com.zetcode.service;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;

public class ReadPage {

    public static String read(String webPage) {

        String html = "";
        
        try {
            html = Jsoup.connect(webPage).get().html();
        } catch (IOException ex) {
            Logger.getLogger(ReadPage.class.getName()).log(
                    Level.WARNING, null, ex);
        }
        
        return html;
    }
}

The read() method uses Jsoup to read the contents of the given web page.

Vaadin TextArea
Figure: Vaadin TextArea

In this tutorial, we have showed the basics of the Vaadin TextArea component. You might also be interested in the related tutorials: Jsoup tutorial, Vaadin ComboBox tutorial, Vaadin Grid tutorial, Vaadin Button tutorial, Vaadin Slider tutorial, Vaadin CheckBox tutorial, Java tutorial.