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.

tutorial.asciidoc 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. ---
  2. title: Vaadin Tutorial
  3. order: 1
  4. layout: page
  5. subnav_auto_list_numbers: true
  6. subnav:
  7. - title: Overview
  8. href: '#framework.tutorial.overview'
  9. subnav:
  10. - title: Installing the Development Tools
  11. href: '#framework.tutorial.overview.tools'
  12. - title: Creating a project using an archetype
  13. href: '#framework.tutorial.archetype'
  14. - title: Adding a demo "backend"
  15. href: '#framework.tutorial.backend'
  16. - title: Listing entities in a Grid
  17. href: '#framework.tutorial.grid'
  18. - title: Creating live filtering for entities
  19. href: '#framework.tutorial.filtering'
  20. - title: Creating a form to edit Customer objects
  21. href: '#framework.tutorial.form'
  22. subnav:
  23. - title: Creating a form using Vaadin Designer
  24. href: '#framework.tutorial.form.designer'
  25. subnav:
  26. - title: Installing Vaadin Designer
  27. href: '#framework.tutorial.form.designer.install'
  28. - title: Creating the form design
  29. href: '#framework.tutorial.form.designer.create'
  30. - title: Creating a form using plain Java
  31. href: '#framework.tutorial.form.java'
  32. - title: Connecting the form to the application
  33. href: '#framework.tutorial.form-connection'
  34. - title: It works! What next?
  35. href: '#framework.tutorial.next'
  36. ---
  37. :sectnums:
  38. [[framework.tutorial]]
  39. = Vaadin Tutorial
  40. This tutorial gives you an overview of how you can use https://vaadin.com/framework[Vaadin Framework] to build single-page web UIs for your Java application.
  41. All you need to start with it is JDK 8 and an https://en.wikipedia.org/wiki/Integrated_development_environment[IDE], such as Eclipse.
  42. While this tutorial is written for Eclipse users, you can use your IDE of choice.
  43. No extensive knowledge of Java is needed, only basic programming skills are required.
  44. [[framework.tutorial.overview]]
  45. == Overview
  46. You'll have your first Vaadin app running in your IDE in a couple of minutes. To complete the whole tutorial, you'll need about 20 to 60 minutes, depending on your existing experience.
  47. Naturally, you can just do the beginning of the exercise or pick any of the steps you want.
  48. To start from a specific step, we have prepared the example code after each step to be downloaded as a zip file.
  49. TIP: The free training videos are an accessible way to get familiar with Vaadin 8. https://vaadin.com/training/courses?type=all[See all courses here!]
  50. In this tutorial, we build a simple customer management system.
  51. It is not a real application; we use an in-memory "back-end", so that you can understand how to hook it to an existing Java based back-end.
  52. We cover the basic Vaadin development and you can use the result as a basis for more experiments with Vaadin, such as using add-ons, your own custom look-and- feel (aka. theme), adding new views, or optimizing the result for mobile support.
  53. [[figure.framework.tutorial.final-ui]]
  54. .UI to be built in the tutorial
  55. image::img/finalUI.jpg[]
  56. If you do not want to do the exercise at all, you can also just https://github.com/vaadin/tutorial/[download the final application] and play around with it.
  57. TIP: In addition to this written tutorial, we have recorded the steps as https://www.youtube.com/playlist?list=PLcRrh9hGNalkZx921iMqZ-jXR0C72eIPR[a series of videos]. You can just watch all the videos or watch the video of each step before you do the same step yourself.
  58. [[framework.tutorial.overview.tools]]
  59. === Installing the Development Tools
  60. The tutorial uses Java 8, so please ensure that you have an up-to-date JDK 8 installed.
  61. Most Linux distributions can use package managers to install JDK8.
  62. Windows and Mac users should download it from http://www.oracle.com/technetwork/java/javase/downloads/index.html[Oracle's Java
  63. SE site].
  64. Also make sure you have the latest version of your IDE.
  65. Eclipse is available in various packages; be sure to download the *Eclipse IDE for Java EE Developers* from http://www.eclipse.org/downloads/[eclipse.org].
  66. [[framework.tutorial.archetype]]
  67. == Creating a Project from an Archetype
  68. The following video shows how to do this step of the tutorial:
  69. video::sMGHSK_97NI[youtube, width="640", height="400"]
  70. As the starting point for the application, we use a Maven archetype called `vaadin-archetype-application`.
  71. Archetypes are project stubs that have some example code and a basic Maven build script.
  72. . Start by choosing "File > New > Maven Project" from the menu.
  73. +
  74. [[figure.framework.tutorial.create-maven-project]]
  75. .Create a new Maven project
  76. image::img/createMavenProject.jpg[width=70%]
  77. +
  78. TIP: If the [guilabel]#Maven Project# is not visible in the menu, you should switch to the _Java EE_ perspective.
  79. You can use the shortcut button in the tool bar or "Window > Perspective" to switch to the perspective.
  80. . The first step in the wizard is good as is for our purpose.
  81. Just click [guibutton]#Next#.
  82. . In the second step, you need to choose the `vaadin-archetype-application` archetype.
  83. +
  84. .Selecting the archetype
  85. image::img/projectWizard2-top.jpg[width=70%]
  86. +
  87. You can first try to find it using the filtering function.
  88. +
  89. If Eclipse has not yet indexed the archetype catalog, you need to manually add the archetype details.
  90. +
  91. TIP: Some Eclipse installations do not automatically add Maven Central to the list of Maven catalogues, resulting in the Vaadin archetypes not being available in the wizard. To fix this, open Eclipse Preferences > Maven > Archetypes and select `Add Remote Catalog`. The URL to the Maven Central catalog file is `http://repo1.maven.org/maven2/archetype-catalog.xml`. After applying the changes, the Vaadin archetypes should be available in the wizard.
  92. +
  93. // +
  94. // .Adding a new archetype
  95. // image::img/projectWizard2-add.jpg[width=70%]
  96. +
  97. Click the [guibutton]#Add Archetype# button.
  98. +
  99. .Settings for a new archetype
  100. image::img/projectWizardAddArchetype-crop.jpg[width=70%]
  101. +
  102. Enter the following values:
  103. +
  104. [guilabel]#Group ID#::
  105. Give `com.vaadin`
  106. [guilabel]#Artifact ID#::
  107. Give `vaadin-archetype-application`
  108. [guilabel]#Version#::
  109. You can find the latest Vaadin version number from https://vaadin.com/framework/releases[vaadin.com/framework/releases].
  110. [guilabel]#Repository URL#::
  111. This can be left blank.
  112. +
  113. And click [guibutton]#OK#.
  114. Now you can select the new archetype from the list.
  115. +
  116. WARNING: Eclipse has a bug in its project wizard.
  117. The `vaadin-archetype-application` may not appear in the listing, even though you added it using the [guibutton]#Add Archetype# button.
  118. If this occurs, close the whole new project wizard and re-open it by selecting "File > New > Maven Project" again.
  119. The archetype then typically appears in the listing and can be found by the filtering functionality.
  120. . In the next wizard step, type in the following fields:
  121. [guilabel]#Group Id#:: `my.vaadin`
  122. [guilabel]#Artifact Id#:: `app`
  123. +
  124. and click [guibutton]#Finish#.
  125. If this is your first Vaadin app, creating a project might take a while, depending on the speed of your network, as Vaadin libraries and other dependencies are being downloaded.
  126. Maven caches them on your local file system.
  127. Creating your next Maven-based Vaadin project will be much faster.
  128. Right click on the newly created project and choose "Run as > Maven Install".
  129. This initiates a full build of your application and finally creates a https://en.wikipedia.org/wiki/WAR_(file_format)[WAR] file into the [filename]#target# directory.
  130. You can deploy the WAR file to your application server.
  131. The first build might take a while, as Maven might again need to download some new modules.
  132. TIP: For the Maven compilation to work you need a JDK to be configured in your
  133. Eclipse in "Window > Preferences > Java > Installed JREs > Add...".
  134. This step is necessary at least on Windows, if you are using a fresh installation of Eclipse or for some other reason haven't configured a JDK to your Eclipse.
  135. The JDK by default installs to [filename]#\Program Files\Java# on Windows.
  136. You can make JDK the default JRE for your Eclipse.
  137. While the build is running, let us have a look at what the archetype created for
  138. you.
  139. You can browse your project resources from the tree structure in the [guilabel]#Project Explorer#.
  140. Maven's [filename]#pom.xml# on top level contains settings for building your project and declares the used dependencies.
  141. Open [guilabel]#Java Resources# and below it [filename]#src/main/java#, the main source directory, and [packagename]#my.vaadin.app#, the main Java package that will contain your Vaadin UI code.
  142. TIP: Eclipse shows all project files in the Project Explorer.
  143. In this case, you can also find your [filename]#.java# files from under the top-level [filename]#src# node.
  144. However, the suggested method is to access them from under the [guilabel]#Java Resources# node, which is optimized for editing Java source code.
  145. The UI code (and the Servlet declaration) used by the application stub can be
  146. found in the [filename]#MyUI.java# file.
  147. Let us read it through to see how it works.
  148. The [methodname]#init()# method of a UI class is triggered when a user enters your web application.
  149. The [classname]#VerticalLayout# is one of the most used layout components, which
  150. are used to position and display other Vaadin components in your UI classes.
  151. The example code creates one [classname]#TextField# to allow the user to input her name and a [classname]#Button# whose click listener dynamically adds a new [classname]#Label# component to the main layout.
  152. In the end of the [methodname]#init()# method, we just configure the main layout and place components into it and set it to be the content of [classname]#MyUI#.
  153. To test your first Vaadin application, right-click on the project and choose "Debug as > Maven build...".
  154. The debug mode is slightly slower than the basic run mode, but it often helps you to figure out what is happening in your application.
  155. [[figure.framework.tutorial.debug-as-maven-build]]
  156. .Starting the server using a Maven target
  157. image::img/debugAsMavenBuild.jpg[]
  158. In the dialog, type `Run in jetty` to the [guilabel]#Name# input and `jetty:run` to the [guilabel]#Goals# input.
  159. [[figure.framework.tutorial.jetty-run]]
  160. .Generating a Maven launch for `jetty:run` target
  161. image::img/debugAsMavenBuild2.jpg[]
  162. Before clicking debug, to make sure debugging works properly, add your Java
  163. project to the source lookup path from the [guilabel]#Source# tab, as it is being done in <<figure.tutorial.creating.add-sources>>.
  164. [[figure.tutorial.creating.add-sources]]
  165. .Adding sources for debugging
  166. image::img/debugAsMavenBuildAddSources.jpg[]
  167. Now click [guibutton]#Debug# to continue.
  168. This will download a small Java web server (if not cached to your local Maven repository), and use it to host your application.
  169. Once the server has started, point your browser to the URL http://localhost:8080/[http://localhost:8080/] to see the running application.
  170. If you make changes to the code, the Jetty server will notice the changes and in
  171. a couple of seconds most changes are automatically deployed.
  172. Reloading the page in your browser will show the changes.
  173. TIP: In some cases your JVM might not allow injecting changes on the fly.
  174. In these cases, Eclipse will complain about "Hot code replacement error".
  175. Just choose to restart the server to get the latest changes.
  176. Many Java developers use a commercial tool called http://zeroturnaround.com/software/jrebel/[JRebel] to make code replacement work better.
  177. Mastering the usage of the Java debugger is also handy to better understand how your application actually works and fixing bugs that all developers write at some point.
  178. As Vaadin is "only" Java code, you can use all of Java's debugging tools, which cannot be done with other UI frameworks where the UI is written (partly) in HTML and/or JavaScript.
  179. Double-click on the line number in the Java editor, for example of the following line in the click listener:
  180. [source,java]
  181. ----
  182. layout.addComponent(new Label("Thanks " + name.getValue()));
  183. ----
  184. Doing so adds a breakpoint to the selected line.
  185. If you then click the button in your browser, the execution of the application will stop on that line.
  186. Eclipse will ask you to enter to _Debugging perspective_.
  187. That way you can step through the execution and inspect the variables.
  188. Clicking on the _play_ icon in the toolbar will continue the execution.
  189. Double-click the same line again to remove the breakpoint.
  190. [[figure.framework.tutorial.breakpoint]]
  191. .Execution in a break point in the button click listener
  192. image::img/debugInBreakPointVariable.jpg[]
  193. Clicking the red square in the [guilabel]#Console# view will terminate the server process.
  194. You can restart it easily form the run/debug history.
  195. You can find that from the small down arrow next to the green play button or bug button (for the debug mode) in the tool bar.
  196. Alternatively, you can use the main menu "Run > Run
  197. history/Debug history > Run in Jetty".
  198. To get back to the _Java EE Perspective_, an Eclipse mode designed for editing Java web app code, click the [guibutton]#Java EE# button in the toolbar.
  199. [[framework.tutorial.backend]]
  200. == Adding a demo "backend"
  201. TIP: Starting from this step directly? https://github.com/vaadin/tutorial/archive/v8-step2.zip[Download the project] for this step, extract the zip file and choose "Import... > Maven > Existing Maven project".
  202. Before getting more into real Vaadin development, let us introduce some domain objects and a "fake backend".
  203. In a real-world application, you will most likely have something similar, implemented with, for example, JPA and EJB or a Spring-based service.
  204. The following video shows how to do this step of the tutorial:
  205. video::qF31jkBFbR0[youtube, width="640", height="400"]
  206. Copy the following three classes from github to your project.
  207. Class names point to the classes hosted in Github.
  208. Copying classes can be done in many ways.
  209. TIP: The fastest way to copy classes using Eclipse is to use your good old
  210. clipboard. Select the text content of the whole class from your browser, choose
  211. "Edit > Copy", focus the node representing the [packagename]#my.vaadin.app# Java package in Eclipse's Java Resources view and choose "Edit > Paste".
  212. Eclipse is smart enough to automatically create a properly named Java file for the class.
  213. * https://raw.githubusercontent.com/vaadin/tutorial/master/src/main/java/my/vaadin/app/CustomerStatus.java[CustomerStatus] - this is a simple enum class
  214. * https://raw.githubusercontent.com/vaadin/tutorial/master/src/main/java/my/vaadin/app/Customer.java[Customer] - this is the main domain object, a basic Java bean that we will be using in our example
  215. * https://raw.githubusercontent.com/vaadin/tutorial/master/src/main/java/my/vaadin/app/CustomerService.java[CustomerService] - this is a simple facade via which you can request and modify [classname]#Customer# instances.
  216. You can think of this as your entry point to your fake database.
  217. In the next steps, we will be using these classes and build a UI around them.
  218. The actual implementation of these classes is not relevant for this tutorial, but feel free to have a look around.
  219. [[framework.tutorial.grid]]
  220. == Listing entities in a Grid
  221. TIP: Starting from this step directly? https://github.com/vaadin/tutorial/archive/v8-step3.zip[Download the project] for this step, extract the zip file and choose "Import... > Maven > Existing Maven project".
  222. Often when you start building a UI for a data-centric application, the first
  223. thing you want to do is to list your data from your back-end.
  224. There are several components and ways in Vaadin to do this.
  225. In this example, we will use the Grid component for tabular presentation of our customers.
  226. The following video shows how to do this step of the tutorial:
  227. video::EBCm-Vf_fUs[youtube, width="640", height="400"]
  228. We start by introducing a [classname]#Grid# to the [classname]#MyUI# class.
  229. We could of course just introduce the Grid as a variable in the [methodname]#init()# method, but we most likely want to refer to it later.
  230. Also, let us get a reference to the [classname]#CustomerService#.
  231. [source,java]
  232. ----
  233. public class MyUI extends UI {
  234. // Add the next two lines:
  235. private CustomerService service = CustomerService.getInstance();
  236. private Grid<Customer> grid = new Grid<>(Customer.class);
  237. // The rest is already there...
  238. @Override
  239. protected void init(VaadinRequest vaadinRequest) {
  240. ...
  241. ----
  242. TIP: If you are new to Java development, you probably do not feel comfortable
  243. with the red compilation error for the line where the [classname]#Grid# got introduced, because of a missing import.
  244. This is easily fixed in Eclipse by using the
  245. menu:Source[Organize Imports] command. Learn its shortcut (kbd:[Ctrl-Shift-O] or
  246. kbd:[CMD-Shift-O] on Macs), you'll be using it a lot in Java development. In
  247. possible class name collisions, always choose the appropriate class from the
  248. _com.vaadin.ui_ package if you want to import core Vaadin UI classes like the
  249. Grid.
  250. To simply list all properties of all Customer objects from the backend service,
  251. replace the init method with the following snippet:
  252. [source,java]
  253. ----
  254. @Override
  255. protected void init(VaadinRequest vaadinRequest) {
  256. final VerticalLayout layout = new VerticalLayout();
  257. // add Grid to the layout
  258. layout.addComponents(grid);
  259. // fetch list of Customers from service and assign it to Grid
  260. List<Customer> customers = service.findAll();
  261. grid.setItems(customers);
  262. setContent(layout);
  263. }
  264. ----
  265. TIP: Again, use the organize imports feature. The List object we use here is
  266. _java.util.List_.
  267. As we'll want to refresh the listing from various places in our application,
  268. extract the customer listing part into its own "updateList" method with the
  269. *public* modifier. The public modifier is handy later when we want to update the
  270. listing from other classes. You can let Eclipse help here by selecting the
  271. relevant lines and using the "quick fix" feature (kbd:[Ctrl+1] or kbd:[Cmd+1] on
  272. Macs). The extracted method call looks like this:
  273. [source,java]
  274. ----
  275. public void updateList() {
  276. List<Customer> customers = service.findAll();
  277. grid.setItems(customers);
  278. }
  279. ----
  280. If you try the application now, you'll see an empty Grid with no columns. To add columns, configure
  281. the Grid using the _setColumns_ method to show the "firstName", "lastName" and
  282. "email" properties.
  283. [source,java]
  284. ----
  285. grid.setColumns("firstName", "lastName", "email");
  286. ----
  287. At this point the body of the MyUI class should look like this (servlet declaration
  288. omitted):
  289. [source,java]
  290. ----
  291. private CustomerService service = CustomerService.getInstance();
  292. private Grid<Customer> grid = new Grid<>(Customer.class);
  293. @Override
  294. protected void init(VaadinRequest vaadinRequest) {
  295. final VerticalLayout layout = new VerticalLayout();
  296. grid.setColumns("firstName", "lastName", "email");
  297. // add Grid to the layout
  298. layout.addComponent(grid);
  299. updateList();
  300. setContent(layout);
  301. }
  302. public void updateList() {
  303. // fetch list of Customers from service and assign it to Grid
  304. List<Customer> customers = service.findAll();
  305. grid.setItems(customers);
  306. }
  307. ----
  308. You can now save your changes to the file and verify the changes from your browser.
  309. You can do this at any point during the rest of the tutorial as well.
  310. [[framework.tutorial.filtering]]
  311. == Creating live filtering for entities
  312. TIP: Starting from this step directly? https://github.com/vaadin/tutorial/archive/v8-step4.zip[Download the project] for this step, extract the zip file and choose menu:Import...[Maven>Existing Maven project].
  313. A search functionality is expected in every modern application and it is
  314. also a nice Vaadin development exercise. Let's add a filtering functionality to
  315. the Customer listing we created in the previous step.
  316. The following video shows how to do this step of the tutorial:
  317. video::kyMaJLUtz3g[youtube, width="640", height="400"]
  318. We'll start by introducing a [classname]#TextField# component as a field to our [classname]#UI# class:
  319. [source,java]
  320. ----
  321. private TextField filterText = new TextField();
  322. ----
  323. In the [methodname]#init()# method, configure the text field to contain a helpful input prompt
  324. and add a text change listener to the field. The exact place of these lines is
  325. not important, but add them, for example, after you have introduced the _layout_
  326. object.
  327. [source,java]
  328. ----
  329. filterText.setPlaceholder("filter by name...");
  330. filterText.addValueChangeListener(e -> updateList());
  331. filterText.setValueChangeMode(ValueChangeMode.LAZY);
  332. ----
  333. TIP: To keep your code more readable, you can use autoformat after you write or
  334. copy paste code snippets. The default keyboard shortcut in Eclipse is
  335. kbd:[Ctrl+Shift+F] or kbd:[Cmd+Shift+F]
  336. As its name implies, the value change listener allows you to react to changes in
  337. the value contained in the text field. It is configured so that the event is fired
  338. lazily while the user is typing, when there is a small pause in the typing.
  339. This makes it perfect for this kind of automatic filtering. When the
  340. user has changed the text, we'll just update the listing calling the _updateList_
  341. method.
  342. To keep the _updateList_ method functional, it should take into consideration
  343. the possible value in the filterText field. Change the line for fetching the
  344. customers into this:
  345. [source,java]
  346. ----
  347. List<Customer> customers = service.findAll(filterText.getValue());
  348. ----
  349. Before adding the text field to the UI, let's improve the usability a bit
  350. and make a short exercise to compose better components from lower level UI
  351. components. The search field can naturally be cleared with the keyboard, but let's
  352. add a clear button next to the text field. Start by adding the following lines
  353. to the init method, for example right after your _filterText_ configuration:
  354. [source,java]
  355. ----
  356. Button clearFilterTextBtn = new Button(VaadinIcons.CLOSE);
  357. clearFilterTextBtn.setDescription("Clear the current filter");
  358. clearFilterTextBtn.addClickListener(e -> filterText.clear());
  359. ----
  360. Vaadin contains a set of built in icons, from which we use the "X" icon,
  361. _VaadinIcons.CLOSE_, here, which most users will recognise as a functionality to clear
  362. the value. If we set the description to a component, it will be shown as a
  363. tooltip for those users who hover the cursor over the button and wonder what to
  364. do with it. In the click listener, we simply clear the text from the field.
  365. Vaadin contains lots of different kinds of layouts. The simplest way to align
  366. the text field and the button next to each other would be to use a
  367. HorizontalLayout. An alternative way we use here is using a CssLayout, which is
  368. a lightweight layout that is easy to customize with css. Even if you wouldn't
  369. want to play with CSS yourself, you can often use one of the existing style
  370. rules in the default https://demo.vaadin.com/valo-theme[_Valo_] theme. The following snippet will create a nice
  371. compact "composition" of both the TextField and the clear button. Add these
  372. lines to the init method right after you configured the _clearFilterTextBtn_:
  373. [source,java]
  374. ----
  375. CssLayout filtering = new CssLayout();
  376. filtering.addComponents(filterText, clearFilterTextBtn);
  377. filtering.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
  378. ----
  379. Finally, *change* the line in the init method that currently adds only the grid,
  380. to add both _filtering_ composition and the _grid_ to the main _layout_ of the
  381. application.
  382. [source,java]
  383. ----
  384. layout.addComponents(filtering, grid);
  385. ----
  386. Now it is a good place to save your changes and try them in your browser.
  387. [[framework.tutorial.form]]
  388. == Creating a form to edit Customer objects
  389. To edit and add Customer objects we need to create a form, that edits the
  390. values in our domain objects. This tutorial has two alternative methods to do
  391. that. Pick either of them.
  392. [[framework.tutorial.form.designer]]
  393. === Creating a form using Vaadin Designer
  394. TIP: Starting from this step directly? https://github.com/vaadin/tutorial/archive/v8-step5.zip[Download the project] for this step, extract the zip file and choose menu:Import...[Maven>Existing Maven project].
  395. The form to edit Customer objects can be built using several methods of which
  396. the visual composition by drag 'n' drop is the most intuitive. Vaadin
  397. Designer is an Eclipse plugin that you can install and do WYSIWYG editing of
  398. your view code. We'll use it to create the form and then hook the editing logic
  399. to it with Java.
  400. TIP: If you are using another IDE or just prefer to compose your user interface
  401. with code, take the alternative step, <<Creating a form using plain Java>>,
  402. where the CustomerForm is composed using plain Java code.
  403. [[framework.tutorial.form.designer.install]]
  404. ==== Installing Vaadin Designer
  405. Vaadin Designer comes as an integrated part of Vaadin Plugin for Eclipse. It can
  406. be installed easily via Eclipse Marketplace. Choose menu:Help[Eclipse Marketplace]
  407. In the dialog, just search for Vaadin and click _install_ to mark it for
  408. installation. Clicking _Install Now_ will take you to choose the modules you want
  409. and accept the license agreement.
  410. [[figure.framework.tutorial.plugin-install]]
  411. .Selecting Vaadin Plugin for Eclipse for installation in Eclipse Marketplace
  412. image::img/pluginEclipseMarketPlace2.jpg[]
  413. If you get a security warning about the software containing unsigned content,
  414. just accept the warning by clicking OK. After installation, Eclipse asks if you
  415. want to restart. Click Yes.
  416. TIP: When you use Vaadin Designer for the first time in the next
  417. step, it will ask for a license key. Get a key from
  418. https://vaadin.com/designer. If you are not willing to buy a license now, just
  419. acquire a trial license.
  420. [[framework.tutorial.form.designer.create]]
  421. ==== Creating the form design
  422. The following screencast will show you how to produce the
  423. _CustomerFormDesign.html_, a design file we need in this tutorial. Use pause and
  424. slow motion to follow better what is being done in the video. Feel free to get creative!
  425. video::zLE2YfLB1MM[youtube, width="640", height="400"]
  426. TIP: At any point of the process, you can also switch to the markup mode where
  427. you can edit the raw content of the .html file. If you wish to take a shortcut
  428. or think you did something wrong when using the designer, you can just
  429. copy-paste the content of https://github.com/vaadin/tutorial/blob/master/src/main/resources/my/vaadin/app/CustomerFormDesign.html[the final state] to your own .html file.
  430. TIP: With Vaadin Framework 8, make following things differently:
  431. For status' NativeSelect Component, set Itemtype as CustomerStatus enum.
  432. Use DateField Component for birthday, instead of PopupDateField.
  433. At this point we only have a static mockup of the actual UI. To implement a
  434. functional form component, we need some Java code as well. Vaadin Designer
  435. automatically creates a similarly named Java class, but a good habit is to never touch the auto-generated file, in this case the
  436. CustomerFormDesign.java file. If you'd introduce a new field to your form,
  437. your changes to CustomerFormDesign would be overridden by the tooling. Instead,
  438. we'll create a class called CustomerForm which inherits from the auto-generated
  439. CustomerFormDesign class.
  440. Start by creating a new Java class with the name CustomerForm. In Eclipse, right
  441. click on the "my.vaadin.app" package and choose menu:New[Class]. Type in the
  442. name _CustomerForm_, define the superclass as
  443. _my.vaadin.app.CustomerFormDesign_ and click _finish_.
  444. From the superclass, we inherit all the UI elements that we named when using
  445. the designer. E.g. by simply referencing to "save" field in the CustomerForm,
  446. we'll have access to the save button we previously created.
  447. We will later need a reference to the currently edited Customer object,
  448. CustomerService and the MyUI that uses this class. Add these fields and a
  449. basic constructor that accepts MyUI as a parameter to the CustomerForm class:
  450. [source,java]
  451. ----
  452. private CustomerService service = CustomerService.getInstance();
  453. private Customer customer;
  454. private MyUI myUI;
  455. public CustomerForm(MyUI myUI) {
  456. this.myUI = myUI;
  457. }
  458. ----
  459. Although the form is not yet fully functional, you might want to see what it
  460. looks like at this point. Add it as a field to the _MyUI_ class:
  461. [source,java]
  462. ----
  463. private CustomerForm form = new CustomerForm(this);
  464. ----
  465. Now let's modify the init method in MyUI to show the form. Let's wrap both the
  466. Grid and the CustomerForm in a horizontal layout and configure the Grid to use
  467. all of the available space more efficiently. Replace the line
  468. *layout.addComponents(filtering, grid);* with the following:
  469. [source,java]
  470. ----
  471. HorizontalLayout main = new HorizontalLayout(grid, form);
  472. main.setSizeFull();
  473. grid.setSizeFull();
  474. main.setExpandRatio(grid, 1);
  475. layout.addComponents(filtering, main);
  476. ----
  477. If you now save your changes and reload your application page in a browser,
  478. you should see your CustomerForm next to the grid that lists your
  479. existing entities.
  480. Let's get back to the CustomerForm. The first thing we'll need is to populate
  481. the options for the select. To add all enum values as valid selections, add the
  482. following line to the constructor:
  483. [source,java]
  484. ----
  485. status.setItems(CustomerStatus.values());
  486. ----
  487. Let's also improve the UX a bit. When building the design, we already
  488. emphasized the save button with a ValoTheme.BUTTON_PRIMARY style name. Thus, it
  489. would be natural if the enter-key would do the same action as clicking the
  490. save button. Assign a keyboard shortcut to the save button with this line in the
  491. constructor:
  492. [source,java]
  493. ----
  494. save.setClickShortcut(KeyCode.ENTER);
  495. ----
  496. To finish our form, we need to create a public API that we will use in the next part from MyUI, to pass in a Customer object that the form should edit.
  497. We will also add some logic to actually save the changes.
  498. We'll start by adding a [classname]#Binder# as a field to the [classname]#CustomerForm# class:
  499. [source,java]
  500. ----
  501. private Binder<Customer> binder = new Binder<>(Customer.class);
  502. ----
  503. In the constructor of the CustomerForm class add the following line to configure
  504. the Binder:
  505. [source,java]
  506. ----
  507. binder.bindInstanceFields(this);
  508. ----
  509. This configures the Binder to use all the similary named editor fields in
  510. this form to bind their values with their counterpart in the Customer class.
  511. For example, the _CustomerForm.firstName_ TextField will be bound to the
  512. Customer.firstName property.
  513. Create a setter method for the Customer field. Just type _setCus_ in the body of the
  514. class and hit autocomplete (kbd:[Ctrl+Space]) and Eclipse will create a method
  515. stub for you. Complete it with the following implementation:
  516. [source,java]
  517. ----
  518. public void setCustomer(Customer customer) {
  519. this.customer = customer;
  520. binder.setBean(customer);
  521. // Show delete button for only customers already in the database
  522. delete.setVisible(customer.isPersisted());
  523. setVisible(true);
  524. firstName.selectAll();
  525. }
  526. ----
  527. In addition to saving the reference of the currently edited Customer object, we are
  528. calling the _Binder.setBean_ method. This will initialise all
  529. fields in the form and automatically update the values in the domain objects as
  530. the corresponding field value changes in the user interface.
  531. TIP: If the naming convention based databinding doesn't fit your needs, you
  532. can use
  533. https://www.vaadin.com/api/com/vaadin/data/fieldgroup/PropertyId.html[PropertyId]
  534. annotation on fields to explicitly declare the edited property.
  535. We'll also want to ensure the form is visible and that focus goes to the
  536. firstName field to improve user experience. As we will be using the form to
  537. edit both new non-persisted objects and existing customers, we will also show
  538. the delete button only for customers that are already persisted in the backend.
  539. The last thing we need to do is to handle save and delete button clicks. Add
  540. the following methods to the CustomerForm class:
  541. [source,java]
  542. ----
  543. private void delete() {
  544. service.delete(customer);
  545. myUI.updateList();
  546. setVisible(false);
  547. }
  548. private void save() {
  549. service.save(customer);
  550. myUI.updateList();
  551. setVisible(false);
  552. }
  553. ----
  554. Finally, we'll add listeners to the buttons to call these methods. Adding these
  555. simple lambda expressions to the constructor will take care of that:
  556. [source,java]
  557. ----
  558. save.addClickListener(e -> this.save());
  559. delete.addClickListener(e -> this.delete());
  560. ----
  561. TIP: For a truly re-usable form component in a real life project, you'd want to
  562. introduce an interface to replace the myUI field or, even better, use an event
  563. system like https://vaadin.com/wiki/-/wiki/main/Events+and+contexts[CDI events]
  564. to completely decouple the components. We'll leave that out of this tutorial for
  565. simplicity.
  566. [[framework.tutorial.form.java]]
  567. === Creating a form using plain Java
  568. TIP: Starting from this step directly? https://github.com/vaadin/tutorial/archive/v8-step5.zip[Download the project] for this step, extract the zip file and choose menu:Import...[Maven>Existing Maven project].
  569. This is an alternative step to the <<Creating a form using Vaadin Designer>>,
  570. where you'll build the form UI programmatically in plain Java. If you already
  571. completed the step using Vaadin Designer, you can proceed to
  572. <<Connecting the form to the application>>.
  573. The following video shows how to create a form using plain Java:
  574. video::jOhkclbdwp0[youtube, width="640", height="400"]
  575. Start by creating a new Java class with the name CustomerForm. In Eclipse right
  576. click on the "my.vaadin.app" package and choose menu:New[Class]. Type in the
  577. name _CustomerForm_, define the superclass as _com.vaadin.ui.FormLayout_ and
  578. click _finish_.
  579. In the form, we'll need editor fields for each property in our Customer domain
  580. class. There are different kinds of fields in Vaadin for editing different kinds
  581. of properties. In this example, we'll use a TextField, a DateField and a
  582. NativeSelect. Add the following field declarations and action buttons as Java
  583. fields to the CustomerForm:
  584. [source,java]
  585. ----
  586. private TextField firstName = new TextField("First name");
  587. private TextField lastName = new TextField("Last name");
  588. private TextField email = new TextField("Email");
  589. private NativeSelect<CustomerStatus> status = new NativeSelect<>("Status");
  590. private DateField birthdate = new DateField("Birthday");
  591. private Button save = new Button("Save");
  592. private Button delete = new Button("Delete");
  593. ----
  594. Also, we will later need a reference to the currently edited Customer object,
  595. CustomerService and the MyUI that uses this class. Add these fields and a
  596. basic constructor that accepts MyUI as a parameter to the CustomerForm class:
  597. [source,java]
  598. ----
  599. private CustomerService service = CustomerService.getInstance();
  600. private Customer customer;
  601. private MyUI myUI;
  602. public CustomerForm(MyUI myUI) {
  603. this.myUI = myUI;
  604. setSizeUndefined();
  605. HorizontalLayout buttons = new HorizontalLayout(save, delete);
  606. addComponents(firstName, lastName, email, status, birthdate, buttons);
  607. }
  608. ----
  609. In the constructor we make the form size undefined, which practically means it
  610. will consume the minimum space defined by its content. Then we'll just add all
  611. the fields to the CustomerForm and add action buttons to the bottom - side-by-side
  612. using a HorizontalLayout. Although the form is not yet fully functional, you
  613. might want to see what it looks like at this point. Add it as a field to the MyUI
  614. class:
  615. [source,java]
  616. ----
  617. private CustomerForm form = new CustomerForm(this);
  618. ----
  619. Now let's modify the init method in MyUI to show the form. Let's wrap both the
  620. Grid and the CustomerForm in a horizontal layout and configure the Grid to use
  621. all of the available space more efficiently. Replace the line
  622. *layout.addComponents(filtering, grid);* with the following:
  623. [source,java]
  624. ----
  625. HorizontalLayout main = new HorizontalLayout(grid, form);
  626. main.setSizeFull();
  627. grid.setSizeFull();
  628. main.setExpandRatio(grid, 1);
  629. layout.addComponents(filtering, main);
  630. ----
  631. When you now save your changes and reload your application page in your browser,
  632. you should see your CustomerForm next to the grid that lists your
  633. existing entities.
  634. Let's get back to the CustomerForm. The first thing we'll need is to populate
  635. the options for the select. To add all enum values as valid selections, add the
  636. following line to the constructor:
  637. [source,java]
  638. ----
  639. status.setItems(CustomerStatus.values());
  640. ----
  641. Let's also improve the UX a bit. The most common thing your users will want to
  642. do with this kind of form is to save it. Let's decorate the button with a style
  643. name that makes it more prominent in the UI and give it a keyboard shortcut -
  644. simply an enter hit in this case:
  645. [source,java]
  646. ----
  647. save.setStyleName(ValoTheme.BUTTON_PRIMARY);
  648. save.setClickShortcut(KeyCode.ENTER);
  649. ----
  650. To finish our form, we need to create a public API that we will use in the next
  651. part from the MyUI, to pass in a Customer object that the form should edit. We
  652. will also add some logic to actually save the changes. We'll start by adding a
  653. _Binder_ as a field to the _CustomerForm_ class:
  654. [source,java]
  655. ----
  656. private Binder<Customer> binder = new Binder<>(Customer.class);
  657. ----
  658. In the constructor of the CustomerForm class add the following line to configure
  659. the Binder:
  660. [source,java]
  661. ----
  662. binder.bindInstanceFields(this);
  663. ----
  664. This configures the Binder to use all the similary named editor fields in
  665. this form to bind their values with their counterpart in the Customer class.
  666. For example, the _CustomerForm.firstName_ TextField will be bound to the
  667. Customer.firstName property.
  668. Create a setter method for the Customer field. Just type _setCus_ in the body of the
  669. class and hit autocomplete (kbd:[Ctrl+Space]) and Eclipse will create a method
  670. stub for you. Complete it with the following implementation:
  671. [source,java]
  672. ----
  673. public void setCustomer(Customer customer) {
  674. this.customer = customer;
  675. binder.setBean(customer);
  676. // Show delete button for only customers already in the database
  677. delete.setVisible(customer.isPersisted());
  678. setVisible(true);
  679. firstName.selectAll();
  680. }
  681. ----
  682. In addition to saving the reference of the currently edited Customer object, we are
  683. calling the _Binder.setBean_ method. This will initialise all
  684. fields in the form and automatically update the values in the domain objects as
  685. the corresponding field value changes in the user interface.
  686. TIP: If the naming convention based databinding doesn't fit your needs, you
  687. can use
  688. https://www.vaadin.com/api/com/vaadin/annotations/PropertyId.html[PropertyId]
  689. annotation on fields to explicitly declare the edited property.
  690. We'll also want to ensure the form is visible and that the focus goes to the
  691. firstName field to improve the user experience. As we will be using the form to
  692. edit both new non-persisted objects and existing customers, we will also show
  693. the delete button only for customers that are already persisted in the backend.
  694. The last thing we need to do is to handle save and delete button clicks. Add
  695. the following methods to the CustomerForm class:
  696. [source,java]
  697. ----
  698. private void delete() {
  699. service.delete(customer);
  700. myUI.updateList();
  701. setVisible(false);
  702. }
  703. private void save() {
  704. service.save(customer);
  705. myUI.updateList();
  706. setVisible(false);
  707. }
  708. ----
  709. Finally, we'll add listeners to the buttons to call these methods. Adding these
  710. simple lambda expressions to the constructor will take care of that:
  711. [source,java]
  712. ----
  713. save.addClickListener(e -> this.save());
  714. delete.addClickListener(e -> this.delete());
  715. ----
  716. TIP: For a truly re-usable form component in a real life project, you'd want to
  717. introduce an interface to replace the myUI field or, even better, use an event
  718. system like https://vaadin.com/wiki/-/wiki/main/Events+and+contexts[CDI events]
  719. to completely decouple the components. We'll leave that out of this tutorial for
  720. simplicity.
  721. [[framework.tutorial.form-connection]]
  722. == Connecting the form to the application
  723. TIP: Starting from this step directly? https://github.com/vaadin/tutorial/archive/v8-step6.zip[Download the project] for this step, extract the zip file and choose menu:Import...[Maven>Existing Maven project].
  724. In this part, we'll use the CustomerForm class, which we created in the
  725. previous step, from the MyUI class. We will use it for both editing the existing
  726. customers and creating new ones.
  727. The following video shows how to do this step of the tutorial:
  728. video::eMCWf3PfXLs[youtube, width="640", height="400"]
  729. In the previous part, we already added the form to the _MyUI_ to see what it looks
  730. like. By default, we want it to be invisible, so let's first hide it
  731. by adding this line to the _init_ method of MyUI class:
  732. [source,java]
  733. ----
  734. form.setVisible(false);
  735. ----
  736. To edit the customer chosen from the Grid, add the following selection listener to
  737. the end of the _init_ method:
  738. [source,java]
  739. ----
  740. grid.asSingleSelect().addValueChangeListener(event -> {
  741. if (event.getValue() == null) {
  742. form.setVisible(false);
  743. } else {
  744. form.setCustomer(event.getValue());
  745. }
  746. });
  747. ----
  748. In the listener, we simply take the Customer object of the selected row and pass it to
  749. the CustomerForm for editing. In the previous step, we added a side effect to the
  750. _setCustomer_ method that will bind the domain object to the corresponding fields
  751. and make it visible. If the selection is empty, we'll hide the form.
  752. To allow users to also create new customer records, we'll create a simple "Add
  753. customer button" to the top of the UI, right next to the _filtering_ composition
  754. we have already built from a CssLayout, a TextField and a Button. Introduce the new
  755. Button with a click listener, by adding the following lines to the _init_ method,
  756. right after where you introduced the _filtering_ composition:
  757. [source,java]
  758. ----
  759. Button addCustomerBtn = new Button("Add new customer");
  760. addCustomerBtn.addClickListener(e -> {
  761. grid.asSingleSelect().clear();
  762. form.setCustomer(new Customer());
  763. });
  764. ----
  765. In the click listener, we first clear a possible selection from the grid and then
  766. instantiate a new Customer object and pass that to the form for editing.
  767. To add it beside our _filtering_ composition, we can use a HorizontalLayout to
  768. create a toolbar where we place both components. First, introduce a toolbar like
  769. this after the previously created _addCustomerBtn_:
  770. [source,java]
  771. ----
  772. HorizontalLayout toolbar = new HorizontalLayout(filtering, addCustomerBtn);
  773. ----
  774. And, again, *replace* the line that populates your main layout to add the
  775. toolbar instead of just the filtering composition, which we just moved to the
  776. _toolbar_ layout.
  777. [source,java]
  778. ----
  779. layout.addComponents(toolbar, main);
  780. ----
  781. All planned features are now done. You can save the changes and play around with
  782. the application. If something went wrong, you can also download an example of
  783. https://github.com/vaadin/tutorial[the final application] and see what went wrong.
  784. [[framework.tutorial.next]]
  785. == It works! What next?
  786. Congratulations! Users can now create, read, update and delete customer records
  787. stored in the demo backend and you have completed creating your first CRUD UI
  788. with Vaadin.
  789. If you are an experienced Java developer, you are probably already full of ideas of
  790. how you can use your existing skills and create new shiny web UIs for your
  791. existing Java apps. If you want more ideas of how to create full stack
  792. applications, you might, for example, go through the
  793. http://spring.io/guides/gs/crud-with-vaadin/[Creating CRUD UI with Vaadin] guide
  794. and create a bit similar UI with a real database backend implemented with Spring
  795. Data JPA. We have also collected a couple of other resources for an easy
  796. start in your Vaadin developer career.
  797. * https://vaadin.com/docs/-/part/framework/introduction/intro-overview.html[Vaadin online documentation]
  798. * https://vaadin.com/full-stack-starter[Bakery App Starter for Vaadin Framework 8 and Spring] - A fully functional and tested full stack application for you to use as a starting point and a reference implementation on how business grade Vaadin applications should be made.
  799. * http://spring.io/guides/gs/crud-with-vaadin/[Creating CRUD UI with Vaadin] - the tutorial for your first Vaadin application using a Spring based backend.
  800. * https://github.com/mstahv/jpa-invoicer[Jave EE example app] - a Vaadin app example for creating invoices that uses Java EE backend, Apache DeltaSpike Data for simple JPA layer, OAuth2 based login, PDF generation etc.
  801. * http://vaadin.com/directory[Directory] - a vast source of awesome Vaadin add-ons