You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getting-started-kotlin.asciidoc 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ---
  2. title: Using Vaadin with Kotlin
  3. order: 1100
  4. layout: page
  5. ---
  6. [[getting-started.kotlin]]
  7. = Using Vaadin with Kotlin
  8. You can use Vaadin with the link:https://kotlinlang.org/[Kotlin] language as well. Kotlin offers
  9. awesome tooling which includes a plugin for Intellij IDEA, Eclipse and Netbeans.
  10. The plugin includes the possibility to convert any Java class to Kotlin, therefore the easiest way
  11. is to generate the sample link:https://vaadin.com/maven[Java Vaadin Maven project] and
  12. then convert the [filename]#MyUI# class to Kotlin, by pressing
  13. kbd:[Ctrl+Alt+Shift+K]. The Kotlin plugin will convert the class and include Kotlin Maven plugin
  14. to your [filename]#pom.xml#.
  15. The link:https://github.com/mvysny/karibu-dsl[Karibu-DSL] library offers a more Kotlin-like
  16. API for Vaadin. The library will allow you to create
  17. your UI in a hierarchical way. For more details please see
  18. link:https://kotlinlang.org/docs/reference/type-safe-builders.html[Kotlin Type-Safe Builders].
  19. There is a link:https://github.com/mvysny/karibu-helloworld-application[Karibu Helloworld Application]
  20. sample which demonstrates this technique. It is a standard Gradle project, thus you
  21. only need to `git clone` the project and open it in your IDE. The hierarchical code looks as follows:
  22. [source, kotlin]
  23. ----
  24. @Theme("mytheme")
  25. class MyUI : UI() {
  26. private lateinit var layout: VerticalLayout
  27. @Override
  28. override fun init(vaadinRequest: VaadinRequest) {
  29. layout = verticalLayout {
  30. val name = textField {
  31. caption = "Type your name here:"
  32. }
  33. button("Click Me", {
  34. println("Thanks ${name.value}, it works!")
  35. layout.label("Thanks ${name.value}, it works!")
  36. })
  37. }
  38. }
  39. }
  40. ----
  41. It is also possible to use Kotlin to access your database, allowing you to build full-blown
  42. web applications. For details please see the
  43. link:http://www.vaadinonkotlin.eu/[Vaadin-on-Kotlin] library. Just follow
  44. the Getting Started tutorial there - the tutorial will guide you step-by-step.