Starting a Project

Vaadin supports the Kotlin programming language through the Karibu-DSL library, which contains various extensions to use Vaadin with Kotlin. Perhaps most importantly, the library allows you to create your Vaadin UI in a declarative way using a domain-specific language (DSL).

For a quick introduction to the DSL, to have a form with two text input fields, you define them hierarchically under the init method (a block itself) as follows:

  1. @Route("")
  2. class MyView : KComposite() { (1)
  3. private val root = ui { (2)
  4. verticalLayout {
  5. formLayout { (3)
  6. textField("Name:") { (4)
  7. placeholder = "Last name, first name" (5)
  8. }
  9. textField("Age:") {
  10. width = "5em"
  11. }
  12. }
  13. }
  14. }
  1. A view should normally extend the KComposite component, or a Vaadin layout component (in which case the class is defined in a bit different way).

  2. The ui {} function takes care of nesting the VerticalLayout component, produced by the verticalLayout{} DSL function, correctly into the KComposite, thereby making the VerticalLayout effectively the root component of the view.

  3. A Vaadin FormLayout component.

  4. A Vaadin TextField component with caption given.

  5. Define component attributes. For example, setting the value of the placeholder property calls setPlaceholder() in the TextField component, setting the placeholder text that is shown as a hint when there is no text entered into the field.

You can mix such declarative blocks with normal code, as demonstrated later.

For further information, please visit the following resources:

Cloning a Starter Project

You can create a Kotlin project either from an example project, as described in the following, or by enabling it in an existing Vaadin project, as described later.

To get started, clone an example project repository:

git clone https://github.com/mvysny/karibu10-helloworld-application-maven

A starter project with Maven.

git clone https://github.com/mvysny/karibu10-helloworld-application

A starter project with Gradle.

Import and open the project in your IDE. See the sections for Eclipse IDE, IntelliJ, and NetBeans about importing a project in your IDE.

Example Project Walkthrough

The example application has a view with a text input field and a button. Clicking the button shows a notification with the text in the input field.

Kotlin starter

The Kotlin starter app view

The code looks as follows:

  1. @Route("") (1)
  2. @PWA(name = "Project Base for Vaadin", shortName = "Project Base") (2)
  3. class MainView : KComposite() {
  4. private lateinit var nameField: TextField
  5. private lateinit var greetButton: Button
  6. // The main view UI definition
  7. private val root = ui {
  8. verticalLayout { (3)
  9. // Define a CSS style for the layout element
  10. addClassName("centered-content")
  11. // Create and assign a text field
  12. nameField = textField("Your name")
  13. // Create and assign a button
  14. greetButton = button("Say hello") {
  15. setPrimary();
  16. addClickShortcut(Key.ENTER)
  17. }
  18. }
  19. }
  20. init {
  21. greetButton.onLeftClick {
  22. Notification.show("Hello, ${nameField.value}")
  23. }
  24. }
  25. }
  1. Root route.

  2. Enable running as a progressive web application (PWA).

  3. The view layout.

You can try compiling and running the application online with the Gitpod IDE (registration required).

Application Architecture

Application architecture goes in much the same way with Kotlin as with Java Vaadin applications. To access the database, you can choose Spring Data, JPA, or JDBC directly, or try out the innovative Vaadin-on-Kotlin framework at vaadinonkotlin.eu.

Please see the Beverage Buddy App Starter for a full-stack application example.

Enabling the Kotlin DSL in an Existing Vaadin Project

You can enable Kotlin in an existing Maven or Gradle project as described next.

Maven

The needed Karibu-DSL library is in Maven-Central, so the only thing you need to do is to add Karibu-DSL to your project as a dependency:

  1. <project>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.github.mvysny.karibudsl</groupId>
  5. <artifactId>karibu-dsl</artifactId>
  6. <version>1.0.1</version>
  7. </dependency>
  8. </dependencies>
  9. </project>

Gradle

You can include the Karibu-DSL library into your WAR project by including an appropriate Gradle dependency.

First, in the repositories section:

  1. repositories {
  2. jcenter() // or mavenCentral()
  3. }

Then, in the dependencies section:

  1. dependencies {
  2. compile("com.github.mvysny.karibudsl:karibu-dsl:1.0.1")
  3. }

For the newest version, please see the Karibu-DSL Releases page.

Please see the Karibu-DSL tutorial for more information.

Updated 2021-03-09  Edit this article